Topic: About regExp

This code:

<html>
<body>

<script type="text/javascript">
var patt1=new RegExp("e","g");

do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)
</script>

</body>
</html>

Gives this result :

eeeeeenull

My question is why does it have a "null" in the end. I was also trying to get the number of "e" in the sentence "The best things in life are free" and display it like "It has x e's in this sentence" where x should be the number of letter "e". Anyone can help me on this? Thanks!

Re: About regExp

Well, your code loops through the string. On the second to last loop, it finds an "e", so result isn't null. It then loops again, finds no "e"s, and generates a "null", which gets written out to the browser.

Javascript isn't really my strong point, so there may be more elegant ways to do this, but this code would fix that issue, catching that final null:

<html>
<body>

<script type="text/javascript">
var patt1=new RegExp("e","g");

do
{
    result=patt1.exec("The best things in life are frees");
    if (result != null) document.write(result);
}
while (result!=null)

</script>

</body>
</html>