首页 > 解决方案 > 来自字符串末尾的正则表达式延迟匹配

问题描述

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

人为的例子:

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"

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

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

let regexLazy = /n.+?$/

但结果仍然是一样的:

str.match(regex)         //finds "nd of the world as we know it"

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

标签: regex

解决方案


推荐阅读