In the example below I am trying to capture the text between the two asterixes.
var str="The *rain in SPAIN* stays mainly in the plain";
var patt1=/\*...\*/;
console.log(str.match(patt1));
I’m trying to follow the example here
http://www.regular-expressions.info/examples.html
\Q…\E Matches the characters between \Q and \E literally,
suppressing the meaning of special characters.
But I am having trouble following along
Try
The
\*means the literal “*” character. Then the.means any character and*means any number of times, so.*means “any number of characters”.Optional bonus:
The code above should work fine, but you’ll notice that it matches greedily. So with input
abcd*efgh*ijkl*mnop, the output will be*efgh*ijkl*, whereas you might have preferred the non-greedy match*efgh*.To do this, use
The
?operator indicates non-greediness and ensures the least number of characters possible to get to the next\*are eaten, whereas without the?, the most characters possible to get to the next\*are eaten.To learn more I recommend http://www.regular-expressions.info/repeat.html . In particular read the “laziness instead of greediness” part.