7

I want to replace &amp to & using javascript. Here is the sample code of mine.EmployeeCode could contain &. The EmployeeCode is selected from Datagrid and its showed in "txtEmployeeCode" textbox. But if the EmployeeCode contains any & then it shows &amp into the textbox. How could &amp be removed from EmployeeCode? can anyone help...

function closewin(EmployeeCode) {
     opener.document.Form1.txtEmployeeCode.value = EmployeeCode;
     this.close();
}
1
  • Use the 'replace' function of JavaScript Jan 7, 2014 at 6:08

4 Answers 4

13

With this:

function unEntity(str){
   return str.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}

function closewin(EmployeeCode) {
     opener.document.Form1.txtEmployeeCode.value = unEntity(EmployeeCode);
     this.close();
}

OPTIONAL If you are using jQuery, this will decode any html entity (not only &amp; &lt; and &gt;):

function unEntity(str){
   return $("<textarea></textarea>").html(str).text();
}

Cheers

16
  • He asked for it. Look at the question title Jan 7, 2014 at 6:01
  • I stand corrected. I should really read questions more carefully Jan 7, 2014 at 6:02
  • Regex?! Could you please make it more inefficient? Perhaps jQuery can help.
    – bjb568
    Jan 7, 2014 at 6:03
  • @bjb568 Microoptimization is evil, regex is perfect for this problem Jan 7, 2014 at 6:04
  • thanks for your answer. But i don't know why this is not woring
    – rafat
    Jan 7, 2014 at 6:05
2

Try this:

var str = "&amp;";
var newstring = str.replace(/&amp;/g, "&");

For more information, see MDN's documentation about the String replace() method.

6
  • 2
    1. Regex is a powerful tool 2. It's the only way to make it replace all instances of &amp, instead of just the first one Jan 7, 2014 at 6:05
  • Wha? str.replace('&amp;', "&");
    – bjb568
    Jan 7, 2014 at 6:08
  • 1
    @bjb568 Oh, you criticize without knowing a bit. What you suggest would only replace the first coincidence. With str = "A&amp;B&amp;C" your code fails. Please learn before criticizing Jan 7, 2014 at 6:18
  • MDN "The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match." Nothing about the first one.
    – bjb568
    Jan 7, 2014 at 6:20
  • 1
    @bjb568 oh kid, read well the docs. Or better open your console and eval this: "A&amp;B&amp;C".replace('&amp;', "&"); It doesn't return A&B&C as it should. Jan 7, 2014 at 6:24
1

In case you don't want to replace all of those html entities, you can cheat with this:

var div = document.createElement('textarea');
div.innerHTML = "bla&amp;bla"
var decoded = div.firstChild.nodeValue;

your converted value is now in decoded

see Decode &amp; back to & in JavaScript

-1

A regex-abuse-free method:

 function closewin(EmployeeCode) {
      opener.document.Form1.txtEmployeeCode.value = EmployeeCode.split('&amp').join('&');
      this.close();
 }
1
  • You'll do it again for &lt and &gt. And dont forget that optional ; behind the entities.
    – Rolf
    Feb 20, 2018 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.