Home > Web Front-end > JS Tutorial > body text

How regularity processes input content

php中世界最好的语言
Release: 2018-03-30 11:33:34
Original
1153 people have browsed it

This time I will show you how regular rules process input content, and what are the precautions for regular rules to process input content. The following is a practical case, let's take a look.

What this small script implements is to extract the content in the src in the tag from the input box content, then replace the content in the original position, and then remove the

html tag in the input content. , spaces and other operations

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script>
    window.onload = function () {
      var target = document.getElementById("p1");
      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 &#39;:avator:&#39;;
    }
    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>
<p contenteditable="true" id="p1">abc&nbspefg&nbsp<img src="asdjk/123.jpg"/><span>hi&nbspgk</span><img
    src="ahdkh/124.jpg"/>
  <p>lmnop</p>
  q rst uvw
</p>
<button id="but">更新</button>
</body>
</html>
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Regular implementation in js to separate numbers with spaces every four digits

How to do it in JS Use regular regex to replace multiple spaces in a string with one space

The above is the detailed content of How regularity processes input content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template