用正则表达式格式化html标签的代码

时间:2021-05-18

复制代码 代码如下:
<html>
<head>
</head>
<body>
<style>
#x_edit{
height:320px;
width:540px;
border:1pxsolid#666666;

color:red;

filter:chroma(color="#0f0f0f");
background-color:#f0f0f0;
font-family:verdana;
font-size:11px;
line-height:13px;
margin-left:16px;
overflow:scroll;
border-left:none;
scrollbar-arrow-color:#666666;
scrollbar-base-color:#666666;
scrollbar-darkshadow-color:#f0f0f0;
scrollbar-face-color:#f0f0f0;
}
</style>
<spanonclick="abc()">格式化</span>
<divcontentEditable="true"id="x_edit"style='position:relative;line-height:13px;background-color:#f0f0f0;filter:Chroma(color=#FDFEFF);width:540px;height:320px;overflow:auto;white-space:nowrap;z-index:2;border:0px'>
<P>&lt;html&gt;<BR>&lt;head&gt;<BR>&lt;/head&gt;<BR>&lt;body&gt;<BR>&lt;style&gt;<BR>#x_edit,#x_edit1{<BR>&nbsp;height:320px;<BR>&nbsp;width:540px;<BR>&nbsp;border:1pxsolid#666666;</P>
<P>&nbsp;color:red;</P>
<P>&nbsp;filter:chroma(color="#0f0f0f");<BR>&nbsp;background-color:#f0f0f0;<BR>&nbsp;font-family:verdana;<BR>&nbsp;font-size:11px;<BR>&nbsp;line-height:13px;<BR>&nbsp;margin-left:16px;<BR>&nbsp;overflow:scroll;<BR>&nbsp;border-left:none;<BR>&nbsp;scrollbar-arrow-color:#666666;<BR>&nbsp;scrollbar-base-color:#666666;<BR>&nbsp;scrollbar-darkshadow-color:#f0f0f0;<BR>&nbsp;scrollbar-face-color:#f0f0f0;<BR>&nbsp;}<BR>&lt;/style&gt;<BR>&lt;spanonclick="abc()"&gt;试试获得selection的top,然后加上div的scrollTop,然后除以行高&lt;/span&gt;<BR>&lt;spanid="a"onclick="alert('a')"&gt;a&lt;/span&gt;<BR>&lt;spanid="b"onclick="alert('b')"&gt;b&lt;/span&gt;<BR>&lt;spanid="c"onclick="alert('c')"<BR>&gt;c&lt;/span&gt;<BR>&lt;spanid="d"onclick="alert('d')"&gt;d&lt;/span&gt;<BR>&lt;spanid="e"onclick="alert('e')"&gt;e&lt;/span&gt;<BR>&lt;spanid="f"onclick="alert('f')"&gt;f&lt;/span<BR>&gt;<BR>&lt;spanid="g"<BR>&nbsp;onclick="alert('g')"&gt;g&lt;/span&gt;<BR>&lt;textareaid="x_edit"wrap=off&gt;&lt;/textarea&gt;<BR>&lt;divcontentEditable="true"id="x_edit1"style='position:relative;line-height:13px;background-color:#f0f0f0;filter:Chroma(color=#FDFEFF);width:540px;height:320px;overflow:auto;white-space:nowrap;z-index:2;border:0px'&gt;&lt;/div&gt;<BR>&lt;script&gt;<BR>functionabc(){<BR>&nbsp;&nbsp;&nbsp;//s=x_edit1.innerText;<BR>&nbsp;&nbsp;&nbsp;vars=x_edit.value;<BR>&nbsp;&nbsp;&nbsp;s=s.replace(/\n/gi,"");<BR>&nbsp;&nbsp;&nbsp;//s=s.replace(/&lt;(\w+)(.*?)[^&lt;&gt;]&gt;/gi,"&lt;"+aaa("$1$2")+"&gt;");<BR>&nbsp;&nbsp;&nbsp;s=s.replace(/&lt;(.*?)[^&lt;&gt;]&gt;/gi,"&lt;"+aaa("$1")+"&gt;");<BR>&nbsp;&nbsp;&nbsp;s=s.replace(/\n/gi,"@");<BR>&nbsp;&nbsp;&nbsp;//varLineValue=x_edit1.innerText.split("\r")<BR>&nbsp;&nbsp;&nbsp;/*varLineValue=x_edit.value.split("\r")<BR>for(vari=0;i&lt;LineValue.length;i++){<BR>&nbsp;&nbsp;&nbsp;LineValue[i]=LineValue[i].replace(/(".*")/gi,"$1".replace(/&lt;/gi,"\xef").replace(/&gt;/gi,"\xff"))<BR>&nbsp;&nbsp;&nbsp;LineValue[i]=LineValue[i].replace(/&lt;(.*[^&lt;&gt;])&gt;/gi,"&lt;"+"$1"+"&gt;\r")<BR>&nbsp;&nbsp;&nbsp;LineValue[i]=LineValue[i].replace(/\r\r/gi,"\r")<BR>&nbsp;&nbsp;&nbsp;LineValue[i]=LineValue[i].replace(/(".*")/gi,"$1".replace(/\xef/gi,"&lt;").replace(/\xff/gi,"&gt;"))<BR>}*/<BR>&nbsp;&nbsp;&nbsp;//x_edit1.innerText=LineValue.join("");<BR>&nbsp;&nbsp;&nbsp;//x_edit.value=LineValue.join("");<BR>&nbsp;&nbsp;&nbsp;x_edit.value=s;<BR>&nbsp;&nbsp;&nbsp;//x_edit1.innerText=s;<BR>}<BR>functionaaa(str){<BR>&nbsp;&nbsp;&nbsp;str=str.replace(/\r/gi,"");<BR>&nbsp;&nbsp;&nbsp;returnstr;<BR>}<BR>&lt;/script&gt;<BR>&lt;/body&gt;<BR>&lt;/html&gt;</P>
</div>

<script>
functionabc(){
vars=x_edit.innerText;
s=s.replace(/<\/?(\w+[\s\S]*?)>/gi,function(a){returna.replace(/[\r\n]/gi,"")});
x_edit.innerText=s;
}
</script>
</body>
</html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章