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

JavaScript and AJAX Forum

    
Match then Replace
matthewamzn




msg:3954943
 4:10 pm on Jul 18, 2009 (gmt 0)

When I do both the match and replace functions, my script stops working. How do I do a match and replace sequentially?

var youtube = 'http://www.youtube.com/watch?v=44xb9nb2P7g';
var youtube = youtube.match(/=(\w{11})/g);
var youtube = youtube.replace(/=/,'');

(In the example I'm trying to get the last 11 characters following the equal sign.)

 

astupidname




msg:3955253
 9:22 am on Jul 19, 2009 (gmt 0)

.match() returns either an array of found match/matches or null, so if the returned match is not null, access by index number. Try:

var youtube = 'http://www.youtube.com/watch?v=44xb9nb2P7g';
youtube = youtube.match(/v=(\w+)/);
youtube = (youtube !== null) ? youtube[0].replace(/v=/,'') : 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