欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

正则表达式小脚本(对输入内容进行处理)

程序员文章站 2022-06-23 11:25:00
该小脚本实现的是对输入框内容中取出标签中的src中的内容,然后在原来位置进行替换内容,然后在去除输入内容中的html标签,空格等操作 <...

该小脚本实现的是对输入框内容中取出<img>标签中的src中的内容,然后在原来位置进行替换内容,然后在去除输入内容中的html标签,空格等操作

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
  <script>
    window.onload = function () {
      var target = document.getelementbyid("div1");
      var but = document.getelementbyid("but");
      var value;
      but.onclick = function () {
        value = target.innerhtml;
        result = dealtext(value);
        alert(result);
      };
    };

    function dealtext(msg) {
      var text1 = imageround(msg);
      var text2 = removetag(text1);
      var text3 = removeblank(text2);
      var text4 = changenbsp(text3);
      return text4;
    }


    function dealnum(num) {
      var content = [];
      for (var i = 0; i < num.length; i++) {
        //
        content.push(defaultsrc(num[i]));
      }
      return content;
    }

    function defaultsrc(src) {
      return ':avator:';
    }

    function returnvalue(srcvalue) {
      var num = [];
      var pattern = /([^\/]+)\./;
      var src;
      for (var i = 0; i < srcvalue.length; i++) {
        src = pattern.exec(srcvalue[i])[1];
        num.push(src);

      }
//      alert(num);
      var source = dealnum(num);
      return source;

    }


    function imageround(text) {
      var reg2 = /<img.*?\>/gi;
      var s = text.match(reg2);
      var num = returnvalue(s);
      var i = 0;
      var content = text.replace(reg2, function () {
        return num[i++];
      });
      return content;
    }

    function removetag(text) {
      var text1;
      var reg = /<.*?>|<.*?\/>/g;
      text1 = text.replace(reg, "");
      return text1;
    }

    function removeblank(text) {
      var text1;
      var reg = /\s+/g;
      text1 = text.replace(reg, "");
      return text1;
    }

    function changenbsp(text) {
      var text1;
      var reg = /&nbsp/g;
      var reg1 = /\;/g;
      text1 = text.replace(reg, ' ');
      text1 = text1.replace(reg1, "");
      return text1;
    }
  </script>
</head>
<body>
<div contenteditable="true" id="div1">abc&nbspefg&nbsp<img src="asdjk/123.jpg"/><span>hi&nbspgk</span><img
    src="ahdkh/124.jpg"/>

  <div>lmnop</div>
  q rst uvw
</div>
<button id="but">更新</button>
</body>
</html>