Reputation: 1103
I'm writing some function that switches urls into their embed url. So, I have a two-dimensional array that has the original url and the embed url.
I'm trying to use RegExp to make sure this is a valid URL, but it just won't work..
This is a code that worked:
arr[x].match(/^(http:\/\/){0,1}(www.){0,1}(allmyvideos.net\/){1}([a-z0-9])+$/ig);
While this code won't work:
var sites = [
["allmyvideos.net/VIDID", "allmyvideos.net/embed-VIDID"]
];
var arr = document.getElementById('original').value.split("\n");
for(var x in arr)
{
var regex = new RegExp("/^(http:\/\/){0,1}(www.){0,1}(" + sites[0][0].replace("VIDID", "").replace(/[\/]/g, "\\/") + "){1}([a-z0-9])+$/ig");
regex.test(arr[x]);
}
What am I doing wrong here that the second approach is not working?
Upvotes: 0
Views: 42
Reputation: 7427
The new RegExp()
constructor form doesn't use the surrounding /
characters:
var re = new RegExp("thing to check", "ig");
re.test("THING to CHECK")
// => true
Upvotes: 2