Number of occurences using *



An asterisk * in a pattern indicates that the preceding character should appear zero or more times. We will now repeat the preceding example with * in place of ? in the pattern.

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 7
Number of occurrences with *
********************************************************
*/
var pattern1=new RegExp("fo*d");
var string1 = new Array(5);
string1[0] = "food";
string1[1] = "fod";
string1[2] = "fd";
string1[3] = "foooood";
string1[4] = "fud";
var i;
for(i=0; i<=4; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> match found with fo*d ","<br\>");
}
else
{
document.write(string1[i], " -> match not found with fo*d","<br\>");
}
}
14 Regular Expression in Javascript
//-->
</script>
</body>
</html

Try this Example online


You can try this example online at - here .


This gives out the following result

food -> match found with fo*d fod -> match found with fo*d fd -> match found with fo*d foooood -> match found with fo*d fud -> match not found with fo*d

Notice the difference between the anchors + and *. While regular expression "fo*d" will match the string "fd", the regular expression "fo+d" will not match the string "fd".