RSS

Replace All in Javascript

04 May

Hi all, we very well know the string operation, replace in javascript, to replace a string pattern in a string with another pattern, ex:

madan.replace(“d”, “dh”); which returns madhan but the thing is it replaces only the first occurence of the patters, to say in details, it will not do replace all.. To achieve that simply include the g modifier like

dhanush.replace(/h/g, “hh”); This will replace all occurences of “h” with “hh”.

Ex: var str = “this is a test string and now test”;

str.replace(/test/g, “tested”);   –   this is a tested string and now tested

 
Leave a comment

Posted by on May 4, 2011 in Javascript

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: