javascript – 正则表达式中的更正
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 正则表达式中的更正,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5215字,纯文字阅读大概需要8分钟。
内容图文
我有一个内容,其中包含内容中的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或逗号或空格时,才能停止正则表达式标识.但是,如果有任何HTML标记或其他< ins>< / ins>在另一个< 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标记附近停止.
解决方法:
这对于正则表达式来说真的太过分了 – 如果你想在将来改变某些东西,它将会严重无法维护并且难以纠正.使用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/>");
});
#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>
内容总结
以上是互联网集市为您收集整理的javascript – 正则表达式中的更正全部内容,希望文章能够帮你解决javascript – 正则表达式中的更正所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。