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

网络编程 2021-07-05 10:07www.168986.cn编程入门
代码如下:

<html>
<head>
</head>
<body>
<style>
#x_edit{
  height:320px;
  width:540px;
  border:1px solid #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>
<span onclick="abc()">格式化</span>
<div contentEditable="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:1px solid #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;span onclick="abc()"&gt;试试获得selection的,然后加上div的scrollTop,然后除以行高&lt;/span&gt;<BR>&lt;span id="a" onclick="alert('a')"&gt;a&lt;/span&gt;<BR>&lt;span id="b" onclick="alert('b')"&gt;b&lt;/span&gt;<BR>&lt;span id="c" onclick="alert('c')"<BR>&gt;c&lt;/span&gt;<BR>&lt;span id="d" onclick="alert('d')"&gt;d&lt;/span&gt;<BR>&lt;span id="e" onclick="alert('e')"&gt;e&lt;/span&gt;<BR>&lt;span id="f" onclick="alert('f')"&gt;f&lt;/span<BR>&gt;<BR>&lt;span id="g"<BR>&nbsp;onclick="alert('g')"&gt;g&lt;/span&gt;<BR>&lt;textarea id="x_edit" wrap=off&gt;&lt;/textarea&gt;<BR>&lt;div contentEditable="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>function abc(){<BR>&nbsp;&nbsp;&nbsp; //s=x_edit1.innerText;<BR>&nbsp;&nbsp;&nbsp; var s=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; //var LineValue=x_edit1.innerText.split("\r")<BR>&nbsp;&nbsp;&nbsp; /var LineValue=x_edit.value.split("\r")<BR>for(var i=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>function aaa(str){<BR>&nbsp;&nbsp;&nbsp; str=str.replace(/\r/gi," ");<BR>&nbsp;&nbsp;&nbsp; return str;<BR>}<BR>&lt;/script&gt;<BR>&lt;/body&gt;<BR>&lt;/html&gt;</P>
</div>

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

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by