homepage Welcome to WebmasterWorld Guest from 54.205.160.82
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
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
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)

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved