简体   繁体   English

查找正则表达式单词的最后一次出现

[英]Find Last Occurrence of Regex Word

How can I find the last occurrence of a word with word boundaries?如何找到带有单词边界的单词的最后一次出现? I created a regex expression of /\btotal\b/ for the word.我为这个词创建了/\btotal\b/的正则表达式。 How would I use search() to find the last occurrence of this expression?我将如何使用 search() 来查找此表达式的最后一次出现? Thanks in advance for the help!在此先感谢您的帮助!

You can use negative lookahead to get the last match:您可以使用负前瞻来获得最后一场比赛:

/(\btotal\b)(?!.*\b\1\b)/

RegEx Demo 1正则表达式演示 1

RegEx Demo 2正则表达式演示 2

(?!.*\\1) is negative lookahead to assert that captured group #1 ie total word is NOT present ahead of the present match. (?!.*\\1)是否定前瞻,以断言捕获的组 #1 即total单词在当前匹配之前不存在。

Without using the lookaheads but using the same regex (having applied the g , ie global, flag), the option would be to match the string with regex and get the last match.不使用前瞻但使用相同的正则表达式(已应用g ,即全局标志),选项是将字符串与正则表达式匹配并获得最后一个匹配项。

var matches = yourString.match(/\btotal\b/g);
var lastMatch = matches[matches.length-1];

I like @anubhava's answer.我喜欢@anubhava 的回答。 And in case your string might contain line breaks, you can try the following Negative Lookahead:如果您的字符串可能包含换行符,您可以尝试以下 Negative Lookahead:

(\btotal\b)(?!.*[\r\n]*.*\1)

See an example on regex101.一个例子在regex101。 Also see below, we replace the last occurrence of total with <TOTAL>另见下文,我们用<TOTAL>替换最后一次出现的total

var str = `
something total abc 
total 


foo total anything here
total



Anything total done!
`;

var finalStr = str.replace(/(\btotal\b)(?!.*[\r\n]*.*\1)/, "<TOTAL>");

console.log(finalStr)

And you will get:你会得到:

something total abc 
total 


foo total anything here
total



Anything <TOTAL> done!

@anubhava answer is correct for single line inputs, but the idea is correct thank you for sharing it! @anubhava 答案对于单行输入是正确的,但这个想法是正确的,谢谢分享!

I think his answer can be improved (but please do correct me if I'm wrong) with version below:我认为他的回答可以改进(但如果我错了,请纠正我)以下版本:

/\b(total)\b(?![\s\S]*\b\1\b)/

The difference here is that [\\s\\S]* will effectively match any character , new lines included.这里的区别在于[\\s\\S]*将有效匹配任何字符,包括新行。 Also word boundary \\b are not really needed in the capturing group.在捕获组中也不需要单词边界\\b

In the linked 2nd example by @anubhava it's matching only the very first total word found because the pattern (?!.*\\b\\1\\b) will not consider newlines.在@anubhava 链接的第二个示例中,它仅匹配找到的第一个total单词,因为模式(?!.*\\b\\1\\b)不会考虑换行符。 You can verify by adding the word total somewhere at the beginning of the sample mail text.您可以通过在示例邮件文本开头的某处添加单词total进行验证。

Demo link: https://regex101.com/r/VlhRWM/3演示链接: https : //regex101.com/r/VlhRWM/3

I'm just a bit worried about performance of the [\\S\\s]* pattern.我只是有点担心[\\S\\s]*模式的性能。

Also I think that the reported errors in comments, for not found matching group, are related to the regex ECMAScript flavor selected in the linked demo, using PCRE is reporting correctly the match.此外,我认为评论中报告的错误,对于未找到匹配组,与链接演示中选择的正则表达式 ECMAScript 风格有关,使用 PCRE 正确报告匹配。


NB.注意。
This pattern will consider a valid match in the ENTIRE input provided.此模式将考虑提供的 ENTIRE 输入中的有效匹配。 If you need a per line match, you need to enable the /g global flag and use @anubhava answer!如果您需要每行匹配,则需要启用/g全局标志并使用 @anubhava 答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM