homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

regular expression breaking script

 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?


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)

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)



 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)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved