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

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

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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745529135a4631613.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信