简体   繁体   English

Javascript:使用正则表达式在最后一次出现的字符串和另一个字符串之间找到字符串

[英]Javascript: Regex to find string between last occurrence of a string and another string

I want to find the text occurring between the last occurrence of the word "to" in a sentence and another word "Mary". 我想查找句子中最后一个出现的单词“ to”和另一个出现的单词“ Mary”之间的文本。

So, for the sentence 所以,对于这句话

"I went on Friday morning to bed, then to arrive for that thing and then to beloved Mary" “我星期五早上去睡觉,然后去做那件事,然后爱上了玛丽”

My regex should match the string "beloved". 我的正则表达式应匹配字符串“ beloved”。

I've tried the following code: 我尝试了以下代码:

var str = "I went on Friday morning to  bed, then to arrive for that 
thing and then to beloved Mary";
var r = /to\b(.*)Mary/g
var match = r.exec(str);
console.log(match);

And am using the global flag as I want to match all occurrences of the word "to". 并且正在使用全局标志,因为我想匹配单词“ to”的所有出现。 I'm expecting it to return an array of strings, with one of them being the string "beloved" but the output I get is: 我期望它返回一个字符串数组,其中一个是字符串“ beloved”,但是我得到的输出是:

["to bed, then to arrive for that thing and then to beloved Mary", " bed, then to arrive for that thing and then to beloved "] [“上床,然后为了那个东西而到心爱的玛丽”,“上床,然后为了那个东西而到心爱的人”]

Jsbin here: Jsbin在这里:

http://jsbin.com/puyijo/edit?js,console http://jsbin.com/puyijo/edit?js,控制台

Any suggestions? 有什么建议么?

EDIT: The word "Mary" is not necessarily at the very end of the string. 编辑:“玛丽”一词不一定在字符串的尽头。

Since you're looking for the last occurrence of to , you can greedily consume any other instances of to with .* : 由于您正在寻找to最后一次出现,因此您可以使用.*贪婪地使用to任何其他实例:

 /.*to\b(.*)Mary/

With the requirement you've described, the /g is not necessary, since there can only be one "last occurrence of to " 根据您所描述的要求, /g不是必需的,因为只能有一个“最后一次出现to

You need to match something before the actual to-Mary sequence: 您需要在实际的to-Mary序列之前进行匹配:

var r = /.+to(.+)Mary/

You also do not use the group flag. 您也不要使用组标志。

Then your matching group is the 1st group: match[1] . 那么您的匹配组是第一组: match[1]

Note that if you might want to trim the result, or add some other chars outside the group in the regex, eg \\s,;\\. 请注意,如果您想修剪结果,或在正则表达式的组外添加其他字符,例如\\s,;\\. ,; \\s,;\\. to not capture those, if you only want the word/s in between. 如果您只想在两者之间加上一个或多个单词,则不捕获它们。

Cheers. 干杯。

Regex parser is greedy by default, so it will first try starting from the first occurrence of to . 正则表达式解析器默认情况下是贪婪的,因此它将首先尝试从首次出现to

You need to discard all the previous characters using .* : 您需要使用.*丢弃所有先前的字符:

/.*to\b *(.*?) *\bMary/g

Try this pattern /(to)(?!.*\\1)(.*)Mary/g 试试这个模式/(to)(?!.*\\1)(.*)Mary/g

  1. '(to)(?!.*\\1)' its matching the last to '(to)(?!.*\\1)'及其配套的最后一个to
  2. (.*) is find the text between to and Mary (.*)toMary之间找到文本

Demo Regex 演示正则表达式

 var str = "I went on Friday morning to bed, then to arrive for that thing and then to beloved Mary"; var r = /(to)(?!.*\\1)(.*)Mary/g; var match = r.exec(str); console.log(match[2]); 

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

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