Welcome to WebmasterWorld Guest from 54.146.221.231

Forum Moderators: open

Message Too Old, No Replies

regular expression breaking script

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

Full Member

10+ Year Member

joined:Jan 27, 2005
posts:229
votes: 0


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 July 23, 2009 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


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 July 24, 2009 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


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)