jquery - Javascript regex test() error: Object has no method 'test' - Stack Overflow

admin2025-04-08  0

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|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|youtu.be)(?:/)(?:watch?v=)?([^&]+) 
has no method 'test' 

Any ideas?

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.|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.|youtu.be)(?:/)(?:watch?v=)?([^&]+) 
has no method 'test' 

Any ideas?

Share Improve this question asked Jul 17, 2013 at 1:09 JimmyBanksJimmyBanks 4,7369 gold badges48 silver badges73 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

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

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

Or this:

var reg = RegExp("^(?:https?://)?(?:www.)?(?:youtube.|youtu.be)(?:/)(?:watch?v=)?([^&]+)");
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744127967a232551.html

最新回复(0)