Freigeben über


JavaScript RegEx Pattern for URLs

Here is a simple JavaScript RegEx pattern for extracting any URL that begins with htttp:

 <script type="text/javascript">

// example string, looks like a tweet to me :)
var str="Awesome resources for HTML5! https://bit.ly/palermo4html5 #HTML5";

// could also be written as:
// var regexUrl = new RegExp("http.[^\s]+", "gi");
var regexUrl=/http.[^\s]+/gi;

// if a match is found, the return type is an array of matches
document.write(str.match(regexUrl));

// output of above line of code:
// https://bit.ly/palermo4html5

</script>