I’m trying to match youtube and vimeo urls on javascript. I’m using regexr to play around and came up with this:
(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+
It works pretty well on regexr, whitespaces aren’t included in the match so it would only match this:
http://youtu.be/ssdfsjlfsfsl
And not this:
http://youtu.be/ssdfsjl someword
But when I test it out on javascript it still matches the url with a whitespace and another word beside it:
var x = new RegExp("(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+")
x.test("http://www.youtube.com/watch?v=FvYo3ZgaQ1c&feature=plcp someword")
Not sure why this is happening, I’ve also tried adding \S or !\s but it doesn’t seem to have any effect.
regex.test(string)returns a boolean value of whether one or more matches are found in the string, so it would (and should) returntruefor"http://www.youtube.com/watch?v=FvYo3ZgaQ1c&feature=plcp someword".If you want to test that your string is a URL and ONLY a url, add some anchors: