RegEx 从字符串末尾开始惰性匹配

Jef*_*man 6 regex

是否可以从字符串末尾执行惰性匹配?

人为的例子:

let str = "It's the end of the world as we know it"
let regex = /n.+$/
str.match(regex)         //finds "nd of the world as we know it"
Run Code Online (Sandbox Code Playgroud)

很简单,它从字符串末尾查找 1 个或多个任意字符,直到找到最后一个“n”(向后)。

然而,现在假设我希望它从字符串末尾查找 1 个或多个任意字符,直到找到第一个“n”(向后)。您可能会认为可以添加“惰性”量词,如下所示:

let regexLazy = /n.+?$/
Run Code Online (Sandbox Code Playgroud)

但结果仍然是一样的:

str.match(regex)         //finds "nd of the world as we know it"
Run Code Online (Sandbox Code Playgroud)

是否可以从字符串末尾开始进行惰性匹配?我缺少什么?谢谢!

归档时间:

查看次数:

690 次

最近记录:

5 年,4 月 前