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?

1
  • Are you generating those &lt and &gt? Commented Jan 15, 2013 at 19:52

3 Answers 3

9
var body = $('#txtHSliderl').val().replace(/&lt/g, "<<"); 
Sign up to request clarification or add additional context in comments.

1 Comment

global, it means it will match the regular expression multiple times rather than just the first occurence.
3

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

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.