我的内容包含内容中的HTML标记。我正在尝试使用图片中提到的条件识别<ins></ins>
和<del></del>
http://i.stack.imgur.com/8iNWl.png
正则表达式为https://regex101.com/r/cE4mE3/30
仅在单个案例中失败,即当<ins></ins>
内的HTML标记或特殊字符无法正确识别时。在上面的正则表达式中,</ins></ins>
位于另一个<ins></ins>
内,因此它在打开<ins>
标记开始之前就已断开。只有当<ins></ins>
之间存在fullstop或逗号或空格时,才能停止正则表达式标识。但是,如果在另一个<ins></ins>
内有任何HTML标记或其他<ins></ins>
标记,则该标识必须继续。
在上述正则表达式中,要选择的组是
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标记附近停止。
答案 0 :(得分:0)
这对于正则表达式来说真的太过分了 - 如果你想在将来改变某些东西,它将会严重无法维护并且难以纠正。使用jQuery,这是一个更好的方法:
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/>");
});
&#13;
#test { margin-bottom: 100px; }
&#13;
<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>
&#13;
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/>");
});
&#13;
#test { margin-bottom: 100px; }
&#13;
<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>
&#13;