Home  >  Article  >  Web Front-end  >  js Html structure to string form display code_javascript skills

js Html structure to string form display code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:59:38988browse
Copy code The code is as follows:

//Html structure converted to string format supports
newline
function ToHtmlString(htmlStr) {
return toTXT(htmlStr).replace(/
|rn|n/g, "
");
}
//Convert Html structure to string form for display
function toTXT(str) {
var RexStr = /<|>|"|'|&| | /g
str = str.replace(RexStr,
function (MatchStr) {
switch (MatchStr) {
case "<":
return "<";
break;
case ">":
return ">";
break;
case """:
return """;
break;
case "'":
return "'";
break;
case "&":
return "&";
break;
case " ":
return " ";
break ;
case " ":
return " ";
break;
default:
break;
}
}
)
return str;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn