Reputation: 907
Below is Example string that I have.
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.
Consider , string with ns:
and text, after is, as a single line.
e.g. 259s: OF A HUMAN BRAIN CELL.
I need regular expression , which returns me lines having ,
Previous Line of Line having span tag if any + Line having span tag + Next Line of having span tag if any
So above string should return me 3 matches.
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
Upvotes: 0
Views: 78
Reputation: 664620
"Previous line having…" is a condition that would need lookbehind, which is not supported by JS. However, the regex would have been overly complicated, so instead just parse it and loop through the lines checking for your matches.
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]);
Upvotes: 1