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();
}
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
rafat
  • 819
  • 2
  • 10
  • 25

4 Answers4

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

Jeremy
  • 1
  • 85
  • 340
  • 366
Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
3

Try this:

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

For more information, see MDN's documentation.

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
  • 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 – MultiplyByZer0 Jan 07 '14 at 06:05
  • Wha? str.replace('&', "&"); – bjb568 Jan 07 '14 at 06:08
  • 1
    @bjb568 Oh, you criticize without knowing a bit. What you suggest would only replace the first coincidence. With `str = "A&B&C"` your code fails. Please learn before criticizing – Edgar Villegas Alvarado Jan 07 '14 at 06:18
  • [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) "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 07 '14 at 06:20
  • 1
    @bjb568 oh kid, read well the docs. Or better open your console and eval this: `"A&B&C".replace('&', "&");` It doesn't return `A&B&C` as it should. – Edgar Villegas Alvarado Jan 07 '14 at 06:24
  • Gah. I they could do a better job of explaining what "some or all" means. Anyway… str.split('&amp').join('&') – bjb568 Jan 07 '14 at 06:39
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

Community
  • 1
  • 1
Jorg
  • 7,219
  • 3
  • 44
  • 65
-1

A regex-abuse-free method:

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