问题
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
http://i.stack.imgur.com/8iNWl.png
The regex is https://regex101.com/r/cE4mE3/30
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>
and
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>
But as there are HTML tags between the identification is stopping near the HTML tag in 1 and 2 groups.
回答1:
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>
来源:https://stackoverflow.com/questions/38869675/correction-in-regex