I try to get the following using js regex:
string</span>string</span>string</span>theStrngIWant
In one sentence i try to get any characters including new lines after the last </span>
I try this pattern /<\\/span>((.|\\n)*)/i
, I know the pattern won't work i just wanted to show what i want to capture:
So after the last </span>
i want to capture anything included in the .
token and because its not include new lines i added \\n
, I think its ok to use greedy without ?
because its the end of the string anyway, Also just to note i did try negative lookahead.
If any one know any regex for this case i will be very thankful.
First, you are asking for trouble if you decide to parse HTML using regex for any production/important code.
That said, for non-critial rough editing purposes, HamZa's pattern works just fine. Here is a slightly more complex, (but more efficient) pattern in the form of a tested JavaScript function:
function processText(text) {
/* # Capture in $1, everything following last SPAN element.
<\/span\s*> # Last SPAN close tag.
( # $1: Everything after last SPAN.
[^<]* # Zero or more non start-of-tag chars.
(?: # Zero or more non-SPAN tags.
< # Allow start of any HTML tag, but
(?!\/?span\b) # only if not start a SPAN tag.
[^<]* # Zero or more non start-of-tag chars.
)* # End zero or more non-SPAN tags.
) # End $1: Everything after last SPAN.
$ # Anchor to end of string.
*/
var re = /<\/span\s*>([^<]*(?:<(?!\/?span\b)[^<]*)*)$/i;
var m = text.match(re);
return (m) ? m[1] : '';
}
The regex is also presented (as a multi-line comment) in free-spacing mode with indentation and comments describing each bite-size regex chunk.
For more info on how to write a good regex, I recommend reading the tuorial at: regular-expressions.info/ . If you would like to become a regex guru, then you would be well served by reading: Mastering Regular Expressions (3rd Edition)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.