Prismjs:正则表达式正向后视等效?
Prismjs: regex positive lookbehind equivalent?
我正在使用 Prism.js,它是语法高亮器,它会高亮显示匹配特定正则表达式的单词。
我想 匹配单词 git
之后的任何单词,所以我尝试像这样使用积极的 lookbehind。
(?<=git )\w+
不幸的是,似乎不支持积极的lookbehind,所以我必须找到它的等效正则表达式。有什么方法可以 匹配单词 git
之后的任何单词而不使用正向回顾吗?
例如,我想在没有正面回顾的情况下执行此操作。
"git checkout master" -> only "checkout"
"git log --graph" -> only "log"
"anything after the word git matches" -> only "matches"
此外,我无法使用组,因为我无法告诉 Prism 选择特定的组。它将始终突出整场比赛。
例如,(?:git )(\w+)
将保存第一组中单词git
之后的任何单词,但它匹配单词git和git之后的单词。所以它会高亮
"git checkout master" -> "git checkout"
"git log --graph" -> "git log"
"anything after the word git matches" -> "git matches"
这不是我想要的。
正如@WiktorStribiżew 在评论中正确提到的那样 "if you can't access a group and have no lookaround features, or \K operator, you can't do what you need." 一般来说,情况会是这样,但是,对 Prism
了解不多通过搜索它的文档,我找到了 this,它在 lookbehind
选项部分指出:
"'lookbehind' : This option mitigates JavaScript’s lack of lookbehind. When set to true, the first capturing group in the regex pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind."
以上应该意味着您可以尝试这样的模式:(\bgit )\w+
当您设置了 lookbehind: true
.
我正在使用 Prism.js,它是语法高亮器,它会高亮显示匹配特定正则表达式的单词。
我想 匹配单词 git
之后的任何单词,所以我尝试像这样使用积极的 lookbehind。
(?<=git )\w+
不幸的是,似乎不支持积极的lookbehind,所以我必须找到它的等效正则表达式。有什么方法可以 匹配单词 git
之后的任何单词而不使用正向回顾吗?
例如,我想在没有正面回顾的情况下执行此操作。
"git checkout master" -> only "checkout"
"git log --graph" -> only "log"
"anything after the word git matches" -> only "matches"
此外,我无法使用组,因为我无法告诉 Prism 选择特定的组。它将始终突出整场比赛。
例如,(?:git )(\w+)
将保存第一组中单词git
之后的任何单词,但它匹配单词git和git之后的单词。所以它会高亮
"git checkout master" -> "git checkout"
"git log --graph" -> "git log"
"anything after the word git matches" -> "git matches"
这不是我想要的。
正如@WiktorStribiżew 在评论中正确提到的那样 "if you can't access a group and have no lookaround features, or \K operator, you can't do what you need." 一般来说,情况会是这样,但是,对 Prism
了解不多通过搜索它的文档,我找到了 this,它在 lookbehind
选项部分指出:
"'lookbehind' : This option mitigates JavaScript’s lack of lookbehind. When set to true, the first capturing group in the regex pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind."
以上应该意味着您可以尝试这样的模式:(\bgit )\w+
当您设置了 lookbehind: true
.