Welcome to WebmasterWorld Guest from 54.205.170.21

Forum Moderators: open

Message Too Old, No Replies

regular expression breaking script

   
8:29 pm on Jul 23, 2009 (gmt 0)

10+ Year Member



I've run into a problem with a regular expression (from the regex library). It stops my script from functioning. Is there a character that needs to be escaped for javascript?

$(function(){

$('#submit').click(function(){
var code = $('#youtube_address').val();

if (code.match(/^http://\w{0,3}.?youtube+\.\w{2,3}/watch\?v=[\w-]{11}/) != undefined) {
alert ('This is a valid youtube address.');

} else {
alert ('This is not a valid youtube address.');
}

});
});

9:25 pm on Jul 23, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Mostly the slashes (which delimit the entire pattern), but also literal periods. This modified pattern might be what you intended:
 if (code.match(/^http:\/\/(\w{0,3}\.)?youtube\.\w{2,3}\/watch\?v=[\w-]{11}/) != undefined) 

Jim

11:50 pm on Jul 24, 2009 (gmt 0)

5+ Year Member



Also, .match() returns either an array of match/matches or null, so checking for undefined would be incorrect, use !== null instead (and always use the true equality === or true inequality !== when comparing to null)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month