I have this string
JavaScript
x
2
1
test = "total abc total foo total anything here totalntotal totalntotalnstartnotal abc total foo total anything here totalnotal abc total foo total anything here totalnstartnotal abc total foo total anything here totaln"
2
How would I go on about matching from the last occurance of start
to the end of the line?
I tried to do this with a negative lookahead but I would always get the first occurance:
(?!$)\nstart[sS]*?$
Expecting match to be characters: 164-219
Advertisement
Answer
You can use
JavaScript
1
2
1
(?ms)^start$(?!.*^start$).*
2
See the regex demo.
Details:
(?ms)
– the.
matches newlines now and the^
/$
anchors now match start/end of any line respectively^
– start of a linestart
– a fixed word$
– end of a line(?!.*^start$)
– a negative lookahead that fails the match if there are any zero or more chars as many as possible followed withstart
as a whole line.*
– the rest of the string.