正则替换文本框的标签

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文本框保留格式 ucoolweb.com</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<textarea name="textContent" cols="100" rows="15" id="textContent"></textarea>
<br />
<input type="button" name="Submit" value="转换输出" onclick="outHTML()" />
<input name="checkbox" type="checkbox" id="checkbox" value="checkbox" checked="checked" />
<label for="checkbox" style="color:#F00">启用转换输出</label>
</form>
<div id="out" style="border:1px solid #F90; padding:10px; background:#FFF8DC; margin-top:10px">请在上面文本框录入带HTML标记的文本测试!</div>
<script type="text/javascript">
/**********************************************************
* author Tang Guohui, created: 2007-12-9 12:30:15
**********************************************************/
function outHTML() {
var getValue=document.getElementById("textContent").value;
var endValue=((getValue.replace(/<(.+?)>/gi,"&lt;$1&gt;")).replace(/ /gi,"&nbsp;")).replace(/\n/gi,"<br>");
/*
(1)转义“<”、“>”
(2)改半角空格为&nbsp;
(3)保留换行
*/

if (document.getElementById("checkbox").checked==true) {
document.getElementById("out").innerHTML=endValue;
}else{
document.getElementById("out").innerHTML=getValue;
}

}//end outHTML function
</script>
</body>
</html>

你可能感兴趣的:(正则替换文本框的标签)