当前位置:主页 > 查看内容

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

发布时间:2021-07-28 00:00| 位朋友查看

简介:复制代码 代码如下: 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-le……
复制代码 代码如下:

<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的top,然后加上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>

原文链接:https://m.jb51.net/article/7163.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:php利用正则表达式取出图片的URL 下一篇:没有了

推荐图文


随机推荐