2

I have a string "#Thisisa string (whichisneat!)"

I want from it the sub-strings "#This" and "a string".

The Regex /#(.*) is (.*)/ gives me this:

This is a string (which
neat!)

What I expected was this:

This
a string (which is neat!)

Why is it matching the second is in the regex and how to get the output I actually expected which is by matching the first is?

var string = "#This is a string (which is neat!)"
var match = string.match(/#(.*) is (.*)/);
alert('1st match: \t' + match[1] + '\n2nd match: \t' + match[2]);
laggingreflex
  • 32,948
  • 35
  • 141
  • 196

1 Answers1

3

It's matching the second "is" because the first part of the regular expression is greedy.

That means that .* will start out as matching the entire string, then the regex evaluator will start to backtrack by removing a character at a time from the match until it can find a match for the rest of the pattern.

You can make the quantifier non-greedy by adding a question mark after it:

/#(.*?) is (.*)/

That will make .*? starting out as matching zero characters, and then a character at a time is added until the rest of the pattern matches.

Demo:

var string = "#This is a string (which is neat!)"
var match = string.match(/#(.*?) is (.*)/);
alert('1st match: \t' + match[1] + '\n2nd match: \t' + match[2]);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005