0

I want to replace all the words that match with a variable in a string, but it doesn't work. I use this code:

var findStr = "hello hi, test, hi";
var textSearch = "hi";
findStr = findStr.replace(textSearch,'<span>'+textSearch+'</span>');

It has to change the color of hi but only the first hi changes, the second one doesn't.

JJJ
  • 32,902
  • 20
  • 89
  • 102
user3722860
  • 189
  • 1
  • 3
  • 11

1 Answers1

-3

The replace method is what you're looking for.

For example: someString.replace("String you want to replace", "with this string")

Jure
  • 799
  • 6
  • 25
  • I use this, but I want to replace a variable with the same variable but with a span tag around it, now it only change the first `hi` but I want it change both. – user3722860 Jun 24 '14 at 18:25