javascript string replace < into <
Hey all, I basically output content into a div like this using jquery:
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...
Any ide开发者_如何学编程as? Many thanks
You could use something like the unescapeHTML()
function in prototype...
Adapted from prototype js source
function unescapeHTML(escapedHTML) {
return escapedHTML.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
}
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));
You need use .html() Because text() converts all html tags
$("#output").html(text);
assign it to variable:
var text = $("#edit").val();
text = text.replace("<","<");
this will work fine
精彩评论