Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace All Occurrences In A String Jquery

Tags:

jquery

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?

like image 389
Nick LaMarca Avatar asked Jan 15 '13 19:01

Nick LaMarca


2 Answers

var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
like image 66
jefffan24 Avatar answered Oct 19 '22 22:10

jefffan24


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

 var body = $('#txtHSliderl').val().replace(/&lt/g, "<<");
like image 45
Mohammad Adil Avatar answered Oct 19 '22 23:10

Mohammad Adil