diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 012a93fb9a2fc0..71222cf7e74f36 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,5 +1,4 @@ -const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; - +const URL_REGEXP = /^((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)$/i; /** * Determines whether the given string looks like a URL. * @@ -13,10 +12,5 @@ const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\ * @return {boolean} Whether or not it looks like a URL. */ export function isURL( url ) { - const match = url.match( URL_REGEXP ); - // The current REGEX pattern will match strings where a valid url is part of it, - // so things like 'this is https://wordpress.com' will return true using `URL_REGEXP.test( url );`. - // This check will ensure that the matched url (the url found) is the same as the original string, - // so the given example will return false. - return match !== null && match.length >= 1 && match[ 0 ] === url; + return URL_REGEXP.test( url ); } diff --git a/packages/url/src/test/index.test.js b/packages/url/src/test/index.test.js index f51192808ac826..d14572627de149 100644 --- a/packages/url/src/test/index.test.js +++ b/packages/url/src/test/index.test.js @@ -37,8 +37,11 @@ describe( 'isURL', () => { [ 'https://wordpress.org/./foo' ], [ 'https://wordpress.org/path?query#fragment' ], [ 'https://localhost/foo#bar' ], + [ 'https:///localhost/foo#bar' ], [ 'mailto:example@example.com' ], [ 'ssh://user:password@127.0.0.1:8080' ], + [ 'file:///localfolder/file.mov' ], + [ 'file:/localfolder/file.mov' ], ] )( 'valid (true): %s', ( url ) => { expect( isURL( url ) ).toBe( true ); } );