4

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

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.