下面是我的示例字符串。
143s: WHAT IS <span>THAT</span>? 144s: HEAR THAT? 152s: EVERYBODY, SHH. SHH. 156s: <span>STAY</span> UP THERE. 163s: [BOAT CREAKING] 165s: WHAT IS THAT? 167s: [SCREAMING] 191s: COME ON! 192s: OH, GOD! 193s: AAH! 249s: OK. WE'VE HAD SOME PROBLEMS 253s: AT THE FACILITY. 253s: WHAT WE'RE ATTEMPTING TO <span>ACHIEVE</span> 256s: HERE HAS <span>NEVER</span> BEEN DONE. 256s: WE'RE THIS CLOSE 259s: TO THE REACTIVATION 259s: OF A HUMAN BRAIN CELL. 260s: DOCTOR, THE 200 MILLION 264s: I'VE SUNK INTO THIS COMPANY 264s: IS DUE IN GREAT PART 266s: TO YOUR RESEARCH.
考虑一下,ns:
字符串和文字后面的字符串为单行。
e.g。 259s: OF A HUMAN BRAIN CELL.
我需要正则表达式,它返回行,
Previous Line of Line having span tag if any + Line having span tag + Next Line of having span tag if any
所以上面的字符串应该返回3个匹配。
1st : 143s: WHAT IS <span>THAT</span>? 144s: HEAR THAT?
2nd : 152s: EVERYBODY, SHH. SHH. 156s: <span>STAY</span> UP THERE. 163s: [BOAT CREAKING]
3rd : 253s: WHAT WE'RE ATTEMPTING TO <span>ACHIEVE</span> 256s: HERE HAS <span>NEVER</span> BEEN DONE
答案 0 :(得分:1)
“上一行有... ”是一个需要lookbehind的条件,JS不支持。但是,正则表达式过于复杂,所以只需解析它并遍历行检查您的匹配。
var text = "…";
var lines = [],
textparts = text.split(/(\d+s:)/);
for (var i=1; i<textparts.length; i+=2)
lines[(i-1)/2] = {
lineNumber: textparts[i].match(/\d+/)[0],
text: textparts[i+1],
hasSpan: /<span>/.test(textparts[i+1])
};
var matchedlines = [];
for (var i=0; i<lines.length; i++)
if (lines[i-1] && lines[i-1].hasSpan && lines[i].hasSpan && …) // or whatever
matchedlines.push(lines[i]);