2014-01-07 65 views
1

我想用0123替代&amp&。这里是mine.EmployeeCode 的示例代码可能包含&。 EmployeeCode从Datagrid中选择,并在“txtEmployeeCode”文本框中显示。但是,如果EmployeeCode包含任何&,那么它会在文本框中显示&amp。如何从EmployeeCode中删除&amp?任何人都可以帮助...替换&amp;&,<lt < and > gt gt to gt在javascript中

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

使用JavaScript –

回答

3

有了这个:

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(); 
} 

可选如果您正在使用jQuery,这将解码任何HTML实体(不仅&amp;&lt;&gt;):

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

干杯

+0

他问道。看问题标题 –

+0

我站好了。我应该更仔细地阅读问题 – MultiplyByZer0

+0

正则表达式?!你能否让它效率更低?也许jQuery可以提供帮助。 – bjb568

0

试试这个:

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

欲了解更多信息,请参阅MDN's documentation

+0

唐的 '替换' 功能不使用正则表达式。 – bjb568

+2

1.正则表达式是一个强大的工具2.它是不是只是第一个 – MultiplyByZer0

+0

什使它取代和放大器的所有实例的唯一途径,? str.replace('&',“' – bjb568

0

如果你不想替换所有这些html实体,你可以作弊这样的:

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

您的转换价值现在是decoded

看到Decode &amp; back to & in JavaScript

-1

一个正则表达式滥用自由法:

function closewin(EmployeeCode) { 
     opener.document.Form1.txtEmployeeCode.value = EmployeeCode.split('&amp').join('&'); 
     this.close(); 
} 
+0

您将再次为&lt&&gt;执行操作。并且不要忘记那是可选的;实体背后。 – Rolf

相关问题