JavaScript > Regular Expressions Modifiers

Finding more than one words in JavaScript

How to find any of the specified alternatives in JavaScript?


By using (x|y) expression we can find any of the specified alternatives.

<p>Click the below button to find any specified between the brackets.</p>
<input type="button" value="Click" onclick="mySearch()" />
<p id="myId"></p>

<script>
    function mySearch() {
        var a = "Tec, TechFunda, ITFunda, TechFunda, ITFun, NetFun, TechFunda, ITFunda, DotNet";
        var b = /(TechFunda|ITFunda)/g;
        var r = a.match(b);
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function mySearch() in the<script>block which is connected to the onclick of the HTML button and there is a string with value "Tec, TechFunda, ITFunda, TechFunda, ITFun, NetFun, TechFunda, ITFunda, DotNet" to the variable a, from the string we need to find the specified alternatives by using (x|y) expression in variable bVariable r returns the specified alternatives between brackets from a string. Onclick of the button "Click" in the HTML code fires the function mySearch() in the <script> block, at the same time (x|y)expression finds the specified alternatives between the brackets {(TechFunda|ITFunda)} from a string with the help of global search and returns the output.

OUTPUT

 

 Views: 3554 | Post Order: 145



Write for us






Hosting Recommendations