8

I have a string of data..

This is a template body for  &lt&ltApproved&gt&gt &lt&ltSubmitted&gt&gt

I want to replace "&lt" with "<<" and "&gt" with ">>"

To replace "&lt" I wrote this code..

 var body = $('#txtHSliderl').val().replace("&lt", "<<");

But it only seems to replace the first occurrence..

This is a template body for  <<&ltApproved&gt&gt &lt&ltSubmitted&gt&gt

How do I replace all occurrences?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Nick LaMarca
  • 7,880
  • 28
  • 90
  • 150

3 Answers3

9
var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
jefffan24
  • 1,326
  • 3
  • 22
  • 35
  • 1
    global, it means it will match the regular expression multiple times rather than just the first occurence. – jefffan24 Jan 15 '13 at 19:52
2

You need to use a regular expression, so that you can specify the global (g) flag:

 var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
Mohammad Adil
  • 44,013
  • 17
  • 87
  • 109
1

just use g like as below

 var body = $('#txtHSliderl').val().replace(/&lt/g, "<<").replace(/&gt/g, ">>");

as you want to replace woth &lt and &gt in your value so you have to applied mathod twice

g is used in this function i.e. replace to replace all occurance of given string instace.

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261