Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex test() error: Object has no method 'test'

I cant get the javascript test() method to work, I keep getting an error, this regular expression is working fine when using the match() function.

This is my JS:

reg="^(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)(?:/)(?:watch?v=)?([^&]+)";

ytl=$('#yt').val();    //this is just an input value
if(reg.test(ytl)){
    alert('works');
}

This is the error I keep getting:

Uncaught TypeError: 
Object ^(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)(?:/)(?:watch?v=)?([^&]+) 
has no method 'test' 

Any ideas?

like image 259
JimmyBanks Avatar asked Feb 16 '23 21:02

JimmyBanks


1 Answers

The test method is defined on RegExp objects. Try this:

var reg = /^(?:https?:\/\/)?(?:www.)?(?:youtube.com|youtu.be)(?:\/)(?:watch?v=)?([^&]+)/;

Or this:

var reg = RegExp("^(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)(?:/)(?:watch?v=)?([^&]+)");
like image 112
p.s.w.g Avatar answered Feb 18 '23 09:02

p.s.w.g