Easy tech stuff!

ReplaceAll for JavaScript

Posted: December 26th, 2007 | Author: Nischal Shetty | Filed under: JavaScript | Tags: , , , , | No Comments »

Unlike java code, JavaScript doesn’t have an exclusive “replaceAll” function. But that doesn’t mean the functionality’s absent in javaScript.

To replace all the occurrences of a particular character or string in a given string we make use of the letter ‘g’ which implies to replace all the occurrences of the given letters.

Ex. var stringToPerformReplace = “3+5-1+2″;

var replacedString = stringToPerformReplace.replace(“+”,”-”,”g”);

The new String will be “3-5-1-2″.

In a similar manner instead of “g”, if you specify “i” it implies “ignorecase”.

Note: You can club both together i.e. “gi” is valid.

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png


Leave a Reply