本文介绍了正则表达式中的更正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的内容包含内容中的HTML标记。我正在尝试识别< ins>< / ins> < del>< / del> 使用图片中提到的条件

I have a content which has content along with HTML tags inside the content. I am trying to identify <ins></ins> and <del></del> with the conditions mentioned in the image

正则表达式是

单一案例失败,即当< ins>< / ins> 中的HTML标记或特殊字符无法正确识别时。在上面的正则表达式中,在另一个< ins>< / ins> < / ins>< / ins> c $ c>因此它在开始< ins> 标记开始之前就已破裂。只有在< ins>< / ins> 之间存在fullstop或逗号或空格时,才能停止正则表达式标识。但是,如果在另一个< ins>< / ins>内有任何HTML标记或其他< ins>< / ins> 标记本身标识必须继续。

It is failing in only single case, that is when there an HTML tag or special character inside <ins></ins> its not identifying correctly. In the above regex there is a </ins></ins> inside another <ins></ins> and hence it is breaking before the start of open <ins> tag. The regex identification must stop only when there is fullstop or comma or space between an <ins></ins>. But if there is any HTML tag or another <ins></ins> tag itself inside another <ins></ins> the identification must continue.

在上述正则表达式中,要选择的组是

In the above regex the groups which are to be selected are

 1. <ins class="ins">ff</ins><del class="del">C</del>om<del class="del"> </del><ins class="ins"><ins class="ins">g</ins></ins><del class="del"> g</del>gp<del class="del">a</del>n<del class="del">y</del>

 2. test<del class="del">test</del><ins class="ins">tik</ins><del class="del">peop</del>man<del class="del"> </del></i><del class="del"> g</del>gp<del class="del">a</del>n<del class="del">y</del>

但由于标识之间存在HTML标记,因此在1和2组的HTML标记附近停止。

But as there are HTML tags between the identification is stopping near the HTML tag in 1 and 2 groups.

推荐答案

这对于正则表达式来说真是太过分了 - 如果你想在将来改变一些东西,它将是严重不可维护,难以纠正。使用jQuery,这是一个更好的方法:

This is really too much for a regex to do - if you want to change something in the future, it's going to be seriously unmaintainable and difficult to rectify. Using jQuery, here's a better way:

var resultsArray = [];

// 1   Loop over all parent > del or parent > ins nodes.
$("p > del,p > ins").each(function(index, element) {
  $(this).map(function(){
    // 1    Check that they have a word or a space before the node.
    if (this.previousSibling &&
        this.previousSibling.nodeValue &&
        /(\w| )/.test(this.previousSibling.nodeValue)) {
      var textBeforeTag = this.previousSibling.nodeValue;
      // 1 Stage complete
      console.log("1: Word or space found before <del/ins> tag - value '" + textBeforeTag + "'");

      // 2a   Check that the node has "del" tags within it
      $(element).children("del").each(function(i, e) {
        // 2a    Stage 2a complete
        console.log("2a: <del> child tag found.");

        // SUCCESS: <ins>/<del> tag starting with word or space contained a <del> tag with any content - add to results
        resultsArray.push(e);
      });

      // 2b   Check that the node has "ins" tags within it
      $(element).children("ins").each(function(i, e) {
        // 2b   Check child value is only one word
        console.log("2b: <ins> child tag found - checking it's inner value ('"+e.innerHTML+"') is only one word without space.");
        if (/^\w$/.test(e.innerHTML)) {
          console.log("2b: Child passed one word test - adding to results.");
          // SUCCESS: <ins>/<del> tag starting with word or space contained a <ins> tag with one word content - add to results
          resultsArray.push(e);
        }
        else console.log("2b: Child failed one word test.");
      });

      // 2c   Check that the node has text of a single word within it
      if (/^\w$/.test(element.innerHTML)) {
        console.log("2c: Parent passed one word test - adding to results.");
        // SUCCESS: <ins>/<del> tag starting with word or space contained text with any content - add to results
        resultsArray.push(element);
      }
    }
  });
});

// Iterate results and add to <div id="test>
resultsArray.forEach(function(e) {
  $("#test").append("Match:");
  $("#test").append("<p>"+e.innerHTML+"</p>");
  $("#test").append("<br/>");
});
#test { margin-bottom: 100px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>The <ins class="ins">ff</ins><del class="del">C</del>om<del class="del"> </del> <ins class="ins">Value<ins class="ins">g</ins></ins><del class="del"> g</del>gp<del class="del">a</del>n<del class="del">y</del> has provided to you all relevant information and access
  as agreed in the terms of the <span style="background-color: rgb(251, 236, 201);" auditor-judgement-id="xzujy8vqwsni">audit engagement letter.enter the text is</span><i>test<del class="del">test</del><ins class="ins">tik</ins><del class="del">peop</del>man<del class="del"> </del></i>
  <del
  class="del">g</del>gp<del class="del">a</del>n<del class="del">y</del>
</p>
<div id="test"></div>
var resultsArray = [];

$("p > del,p > ins").each(function(index, element) {
  $(this).map(function(){
    if (this.previousSibling &&
        this.previousSibling.nodeValue &&
        /(\w| )/.test(this.previousSibling.nodeValue)) {
      var textBeforeTag = this.previousSibling.nodeValue;

      $(element).children("del").each(function(i, e) {
        resultsArray.push(e);
      });

      $(element).children("ins").each(function(i, e) {
        if (/^\w$/.test(e.innerHTML)) {
          resultsArray.push(e);
        }
      });

      if (/^\w$/.test(element.innerHTML)) {
        resultsArray.push(element);
      }
    }
  });
});

// Iterate results and add to <div id="test>
resultsArray.forEach(function(e) {
  $("#test").append("Match:");
  $("#test").append("<p>"+e.innerHTML+"</p>");
  $("#test").append("<br/>");
});
#test { margin-bottom: 100px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>The <ins class="ins">ff</ins><del class="del">C</del>om<del class="del"> </del> <ins class="ins">Value<ins class="ins">g</ins></ins><del class="del"> g</del>gp<del class="del">a</del>n<del class="del">y</del> has provided to you all relevant information and access
  as agreed in the terms of the <span style="background-color: rgb(251, 236, 201);" auditor-judgement-id="xzujy8vqwsni">audit engagement letter.enter the text is</span><i>test<del class="del">test</del><ins class="ins">tik</ins><del class="del">peop</del>man<del class="del"> </del></i>
  <del
  class="del">g</del>gp<del class="del">a</del>n<del class="del">y</del>
</p>
<div id="test"></div>

这篇关于正则表达式中的更正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 05:07