Try
<script type="text/javascript">
var str=">1 people>9 people>1u people";
document.write(str.match(/>.*people/img).length);
</script>
at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_dot.
This code should return an array of size 3 but it return array of size 1.
Where is the problem?
JeremyP
87.4k15 gold badges129 silver badges164 bronze badges
asked Oct 3, 2011 at 9:47
user794624
3771 gold badge5 silver badges11 bronze badges
1 Answer 1
The .* part of your regexp is being "greedy" and taking as many characters as it can, in this case returning the entire string as a single match.
Write it like this instead, with a trailing ?:
str.match(/>.*?people/img)
See the section describing "?" in the Mozilla Developer Network JS Reference.
answered Oct 3, 2011 at 9:51
Alnitak
341k72 gold badges420 silver badges503 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js