9

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
  • 37,241
  • 25
  • 195
  • 267
Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152

3 Answers3

9
var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
jefffan24
  • 1,326
  • 3
  • 20
  • 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
3

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

 var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
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
  • 175,020
  • 35
  • 237
  • 263