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

纯洁美丽。它化做雪花,用自己洁白的生躯覆盖在大地上。此时,一切都是银装素裹,一副冰清玉洁的模样。光秃秃的树枝上缀满了朵朵洁白的 "梨花 ",风一吹,花儿纷纷飘落,好似一只只银白色的蝴蝶在空中偏偏起舞。 "忽如一夜春风来,千树万树梨花开 ",这句诗不正是赞美冬天的吗?

该小脚本实现的是对输入框内容中取出<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 = / /g;
      var reg1 = /\;/g;
      text1 = text.replace(reg, ' ');
      text1 = text1.replace(reg1, "");
      return text1;
    }
  </script>
</head>
<body>
<div contenteditable="true" id="div1">abc efg <img src="asdjk/123.jpg"/><span>hi gk</span><img
    src="ahdkh/124.jpg"/>

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

本文正则表达式小脚本(对输入内容进行处理)到此结束。冲动是魔鬼,因为它只会害人。遇事要冷静对待。小编再次感谢大家对我们的支持!

标签: 正则表达式