Javascript javascript字符串替换< 进 <
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5302037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript string replace < into <
提问by Tim
Hey all, I basically output content into a div like this using jquery:
大家好,我基本上使用 jquery 将内容输出到这样的 div 中:
var text = $("#edit").val();
$("#output").text(text);
But I want to turn "<" and ">" into "<" and ">".
但我想转“<” 和“>” 变成“<”和“>”。
text.replace(/</,"<");
doesn't seem to be working for me...
text.replace(/</,"<");
似乎对我不起作用...
Any ideas? Many thanks
有任何想法吗?非常感谢
回答by Quintin Robinson
You could use something like the unescapeHTML()
function in prototype...
你可以unescapeHTML()
在原型中使用类似函数的东西......
Adapted from prototype jssource
改编自原型js源
function unescapeHTML(escapedHTML) {
return escapedHTML.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
}
回答by mattsven
Simple.
简单的。
var needToConvert = 'But I want to turn "<" and ">" into "<" and ">".';
var convert = function(convert){
return $("<span />", { html: convert }).text();
//return document.createElement("span").innerText;
};
alert(convert(needToConvert));
回答by Sergey
assign it to variable:
将其分配给变量:
var text = $("#edit").val();
text = text.replace("<","<");
this will work fine
这会正常工作