Skip to content

Commit

Permalink
update URL regex for improved matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoderat committed Feb 5, 2025
1 parent 559f68f commit 0d51dec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/regex/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const REGEXES = {
url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w\-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)/,
url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+(?::[0-9]+)?|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[^"\s<>{}|\^[\]`]+)*\/?(?:\?[^"\s<>{}|\^[\]`]+)?(?:#[^"\s<>{}|\^[\]`]+)?)?)/,
email:
/(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})/i
};
Expand Down
54 changes: 54 additions & 0 deletions tests/url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, it, expect } from 'bun:test';
import execRegex from '../src/parser/regex/execRegex';

describe('url Tests', () => {
const testCases = [
{
description: 'Case 1: Articles and User Profiles',
url: 'https://example.com/@user-name/best-travel-destinations-2025'
},
{
description: 'Case 2: Comma Separated Values',
url: 'https://example.com/train/los-angeles,california/san-francisco,california/'
},
{
description: 'Case 3: Search Queries',
url: "https://example.com/search?q=bilingual+children's+books"
},
{
description: 'Case 4: Dynamic Routing',
url: 'https://example.com/store/index.php?route=product/product&product_id=999'
},
{
description: 'Case 5: Wiki-style Pages with Parentheses',
url: 'https://example.com/wiki/Toxic_(song)'
},
{
description: 'Case 6: Hash Fragments',
url: 'https://example.com/category/#!/electronics/laptops'
},
{
description: 'Case 7: Special URL Encoded Characters',
url: 'https://example.com/search?q=men%27s%20shoes'
},
{
description: 'Case 8: File Downloads',
url: 'https://example.com/downloads/files/turkish-airlines-cargo-logo.svg'
},
{
description: 'Case 8.1: File Downloads',
url: 'https://example.com/downloads/files/turkish-airlines-cargo-logo.pdf'
},
{
description: 'Case 9: Query Parameters with Multiple Key-Value Pairs',
url: 'https://example.com/search?query=books&id=123&sort=desc&page=2'
}
];

testCases.forEach(({ description, url }) => {
it(description, () => {
const result = execRegex(url, 'url');
expect(result).toEqual(url);
});
});
});

0 comments on commit 0d51dec

Please sign in to comment.