[Javascript] How to check if the given string is a URL?


I want to check if the given string is a URL. How can I do that in Javascript?

1 Answer

3 years ago by

You can use a regex like below to test if the given string is a URL or not.

let regex = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;

console.log(regex.test('https://onecompiler.com/javascript'));
console.log(regex.test('https:onecompiler.com/javascript'));

Run here https://onecompiler.com/javascript/3xnrtarvd

3 years ago by Meera