最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex how to get all occurences between two words in a string - Stack Overflow

programmeradmin1浏览0评论

I am using the following function in order to get the string between two words in a string:

function findStringBetween(str,first,last){
  var r = new RegExp(first+'.*(.*)'+last,'gm');
  var a = str.match(r);     
  return a;
}

But I can not get all occurences of the possible findings.

For example if a sentence (str) like this is the case:

"The sentence which has a lot of words inside. The sentence short inside. And some other sentence to fill this example of mine."

I get this:

var found = findStringBetween(str, 'The', 'inside');
>> ["The sentence which has a lot of words inside. The sentence short inside"]

What I'd like to get is all occurances of findings between the two words "The" and "inside". For the example result would be:

>> ["The sentence which has a lot of words inside",
>> "The sentence short inside"]

Is this possible via regex? if it is not, what can I do to make a fast finding?

Thanks

I am using the following function in order to get the string between two words in a string:

function findStringBetween(str,first,last){
  var r = new RegExp(first+'.*(.*)'+last,'gm');
  var a = str.match(r);     
  return a;
}

But I can not get all occurences of the possible findings.

For example if a sentence (str) like this is the case:

"The sentence which has a lot of words inside. The sentence short inside. And some other sentence to fill this example of mine."

I get this:

var found = findStringBetween(str, 'The', 'inside');
>> ["The sentence which has a lot of words inside. The sentence short inside"]

What I'd like to get is all occurances of findings between the two words "The" and "inside". For the example result would be:

>> ["The sentence which has a lot of words inside",
>> "The sentence short inside"]

Is this possible via regex? if it is not, what can I do to make a fast finding?

Thanks

Share Improve this question edited May 11, 2014 at 15:09 cenk asked May 11, 2014 at 14:53 cenkcenk 1,42915 silver badges29 bronze badges 12
  • Shouldn't the last item be two separate items? – thefourtheye Commented May 11, 2014 at 15:00
  • sorry, the example was a bit misleading. I changed the variables. – cenk Commented May 11, 2014 at 15:00
  • "A" would match lots of possibilities. – cenk Commented May 11, 2014 at 15:01
  • ok between "The" and "inside" how many boundary possibilities can there be for this string? – cenk Commented May 11, 2014 at 15:01
  • In that case, please check my first ment. – thefourtheye Commented May 11, 2014 at 15:02
 |  Show 7 more ments

1 Answer 1

Reset to default 6

Yes, it's possible. The problem is that the regex character "*" (and "+") is "greedy" by default, meaning it will go with the longest possible match. You want the shortest possible match, so make it "lazy" by adding a "?" after it, like so:

function findStringBetween(str, first, last) {
    var r = new RegExp(first + '(.*?)' + last, 'gm');
    return str.match(r);
}
发布评论

评论列表(0)

  1. 暂无评论