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>
%d bloggers like this: