How to find all instances of a pattern in JavaScript ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

To find out out all the instances of a pattern in JavaScript , you can use the regular expression as showb below.

How to find all instances of a pattern in JavaScript ?

<script type="text/javascript">
    var inputStr = "abundantcode is a programing website with abundantcode snippets";
    var Searchpattern = /abundant\w*e/g;
    var item;
    var output = "";
    while ((item = Searchpattern.exec(inputStr)) != null) {
        output += "" + item.index + " is found at " + item[0] + "\n";
    }
    document.write(output);
</script>
Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: