Skip to content

Commit

Permalink
Merge pull request #666 from aswin-s/fix/34324
Browse files Browse the repository at this point in the history
fix: optimise url matching regex
  • Loading branch information
stitesExpensify authored Mar 19, 2024
2 parents fe10326 + 4dc7510 commit aed3f80
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions __tests__/URL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Strict URL validation', () => {
it('correctly tests valid urls', () => {
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i');
expect(regexToTest.test('google.com/')).toBeTruthy();
expect(regexToTest.test('x.com')).toBeTruthy();
expect(regexToTest.test('https://google.com/')).toBeTruthy();
expect(regexToTest.test('ftp://google.com/')).toBeTruthy();
expect(regexToTest.test('we.are.expensify.com/how-we-got-here')).toBeTruthy();
Expand Down
3 changes: 2 additions & 1 deletion lib/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import TLD_REGEX from './tlds';

const ALLOWED_PORTS = '([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])';
const URL_PROTOCOL_REGEX = '((ht|f)tps?:\\/\\/)';
const URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}?((?:www\\.)?[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)+(?:${TLD_REGEX})(?:\\:${ALLOWED_PORTS}|\\b|(?=_))(?!@(?:[a-z\\d-]+\\.)+[a-z]{2,})`;
const URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}?((?:www\\.)?[a-z0-9](?=(?<label>[-a-z0-9]*[a-z0-9])?)\\k<label>\\.)+(?:${TLD_REGEX})\
(?:\\:${ALLOWED_PORTS}|\\b|(?=_))(?!@(?:[a-z\\d-]+\\.)+[a-z]{2,})`;
const addEscapedChar = reg => `(?:${reg}|&(?:amp|#x27);)`;
const URL_PATH_REGEX = `(?:${addEscapedChar('[.,=(+$!*]')}?\\/${addEscapedChar('[-\\w$@.+!*:(),=%~]')}*${addEscapedChar('[-\\w~@:%)]')}|\\/)*`;
const URL_PARAM_REGEX = `(?:\\?${addEscapedChar('[-\\w$@.+!*()\\/,=%{}:;\\[\\]\\|_|~]')}*)?`;
Expand Down

0 comments on commit aed3f80

Please sign in to comment.