SiteMap  | About    

Replace all occurrences of a string

The replace() method in JavaScript searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. But, if a string contain repeated words, replace() method change the first occurrence of the string only.


When you run the code, above script will return "XXX two three one five nine". Here you can see, first 'one' is replaceed by 'XXX' and second 'one' still there. So, how you can change the all occurrences of a string in JavaScript?

Replace all occurrence using Split() and join()

You can replace all occurances of a string using Split() and join() function. The string you entered split() with specified word and again join with replaced word.



Replace all occurrence using RegularExpression

example (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.