Re -Create 025 โ decomposition of regular expressions

Make regular expressions to be tested and understandable
TL; DR: You can destroy complex health to smaller parts to test each part individually and report accurate errors.
The problems that were taken 
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxvii
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-bart-xx-We-have-dreachd-100
https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ix-7rr33ol
Steps 
- ReGEX analysis to determine its logical components.
- Broken Regex into the smallest and is called sub -models for each component.
- Write unit tests for each sub -models to ensure their work properly.
- Combine the sub -models tested in the logic of full verification.
- Refactor code to provide clear error messages for each failure part.
Code 
Before 
function validateURL(url) {
const urlRegex =
/^(https?:\/\/)([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(\/.*)?$/;
// Criptic and untesteable
return urlRegex.test(url);
}
After 
// Step 1: Define individual regex components
const protocolPattern = /^(https?:\/\/)/;
const domainPattern = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const pathPattern = /^\/.*$/;
// Step 2: Write unit tests for each component
describe("Protocol Validation", () => {
test("should pass for http://", () => {
expect(protocolPattern.test("http://")).toBe(true);
});
test("should pass for https://", () => {
expect(protocolPattern.test("https://")).toBe(true);
});
test("should fail for invalid protocols", () => {
expect(protocolPattern.test("ftp://")).toBe(false);
});
});
describe("Domain Validation", () => {
test("should pass for valid domains", () => {
expect(domainPattern.test("example.com")).toBe(true);
expect(domainPattern.test("sub.domain.org")).toBe(true);
});
test("should fail for invalid domains", () => {
expect(domainPattern.test("example")).toBe(false);
expect(domainPattern.test("domain..com")).toBe(false);
});
});
describe("Path Validation", () => {
test("should pass for valid paths", () => {
expect(pathPattern.test("/path/to/resource")).toBe(true);
expect(pathPattern.test("/")).toBe(true);
});
test("should fail for invalid paths", () => {
expect(pathPattern.test("path/to/resource")).toBe(false);
expect(pathPattern.test("")).toBe(false);
});
});
// Step 3: Validate each part and report errors
function validateURL(url) {
if (!protocolPattern.test(url)) {
throw new Error("Invalid protocol. Use http:// or https://.");
}
const domainStartIndex = url.indexOf("://") + 3;
const domainEndIndex = url.indexOf("/", domainStartIndex);
const domain = domainEndIndex === -1 ?
url.slice(domainStartIndex) :
url.slice(domainStartIndex, domainEndIndex);
if (!domainPattern.test(domain)) {
throw new Error("Invalid domain name.");
}
const path = url.slice(domainEndIndex);
if (path && !pathPattern.test(path)) {
throw new Error("Invalid path.");
}
return true;
}
// Step 4: Add integration tests for the full URL validation
describe("Full URL Validation", () => {
test("should pass for valid URLs", () => {
expect(validateURL("https://lesluthiers.com/tour/")).toBe(true);
expect(validateURL("https://bio.lesluthiers.org/")).toBe(true);
});
test("should fail for invalid URLs", () => {
expect(() => validateURL("ftp://mastropiero.com")).
toThrow("Invalid protocol");
expect(() => validateURL("http://estherpsicore..com")).
toThrow("Invalid domain name");
expect(() => validateURL("http://book.warren-sanchez")).
toThrow("Invalid path");
});
});
Write 
Safety 
This is a safe return if you follow the steps carefully.
Each ingredient test ensures that you collide with errors early.
Why is the symbol better? 
The code that has been re -paved is better because it improves reading, maintenance, and the ability to test.
Reexโs destruction into smaller parts makes understanding what each part does.
You can also report specific errors when health verification fails, helping users to fix their inputs.
This is also a great opportunity to apply the testing technology that depends on the test, which gradually increases the complexity by introducing new sub -devices.
How does the objection improve? 
By dividing ReGEX into smaller and meaningful ingredients, you can create a closer maps fee between the requirements of the real world (for example, โURL must have a valid protocolโ) and the symbol.
This reduces mystery and ensures that the code reflects the field of the problem accurately.
restrictions 
This approach may add some general expenses of Regex patterns very simple as their fracture is unnecessary.
Refactor with Amnesty International 
You can use artificial intelligence tools to help identify ReGEX ingredients.
Ask artificial intelligence to explain what every part of Regex does, then guides you to divide it into a smaller pieces that can be tested. For example, you can ask, โWhat is this Regex?โ And follow -up, โHow can I divide it into smaller parts?โ
It is in 2025, no programmer should write new regular expressions anymore.
This mechanical mission of Amnesty International should be left.
Suggested Complex: 1. ReGEX analysis to determine its logical components. RGEX broken to smaller, the sub -models of each component are called. Write unit tests for each sub -models to ensure their work properly. Combine the sub -models tested in the logic of full verification. Refactor code to provide clear error messages for each failure part.
Level
See also 
Credit 
Photo by gerd altmann on Pixabay
This article is part of the re -sale chain.