Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Can anyone tell me how can I check if a string is a URL in JavaScript?

1 Answer

0 votes
by (19.7k points)

 You can use Regexp from Devshed like below:

function validURL(str) {

  var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol

    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name

    '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address

    '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path

    '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string

    '(\\#[-a-z\\d_]*)?$','i'); // fragment locator

  return !!pattern.test(str);

}

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

0 votes
1 answer
0 votes
2 answers
asked Jul 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...