| regular expression breaking script
|
matthewamzn

msg:3958397 | 8:29 pm on Jul 23, 2009 (gmt 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.'); } }); });
|
jdMorgan

msg:3958428 | 9:25 pm on Jul 23, 2009 (gmt 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
|
astupidname

msg:3959269 | 11:50 pm on Jul 24, 2009 (gmt 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)
|
|
|