正则表达式中的更正
我有一个内容,其中包含内容中的HTML标记。 我正在尝试使用图像中提到的条件识别和
http://sofzh.miximages.com/javascript/8iNWl.png
正则表达式是https://regex101.com/r/cE4mE3/30
仅在单一情况下失败,即在内部的HTML标记或特殊字符未正确识别时。 在上面的正则表达式中,在另一个
有一个
,因此它在打开
标记之前就已经打破了。 只有在
之间存在fullstop或逗号或空格时,才能停止正则表达式标识。 但是如果在另一个
有任何HTML标记或其他
标记,则标识必须继续。
在上述正则表达式中,要选择的组是
1. ffCom g ggpany
和
2. testtesttikpeopman ggpany
但由于标识之间存在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 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: child tag found."); // SUCCESS: / tag starting with word or space contained a 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: 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: / tag starting with word or space contained a 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: / tag starting with word or space contained text with any content - add to results resultsArray.push(element); } } }); }); // Iterate results and add to "+e.innerHTML+""); $("#test").append("
"); });
#test { margin-bottom: 100px; }
The ffCom Valueg ggpany has provided to you all relevant information and access as agreed in the terms of the audit engagement letter.enter the text istesttesttikpeopman ggpany