views:

850

answers:

1

I have a word : [lesserthen] , that I need to replace with < zero or more times in a string. I'm using the String.replace method to do so. But the word is only replaced one time in the string, I need to be replaced more then one time. I'm very weak with regular expressions and I'm interested to find a solution for this problem.

Here is the code I've been using:

var wordReplaced="This a text with one [lesserthen], and another [lesserthen]"; wordReplaced=wordReplaced.replace("[lesserthen]","

The result I am aiming is : "This a text with one <, and another <".

But what I got instead is : "This a text with one <, and another [lesserthen]"

+5  A: 

Try using an actual regex (with "g" option) in the replace instead of a search string, for example:

wordReplaced = wordReplaced.replace(/\[lesserthen\]/g,"<");
Marc Novakowski