Search This Blog

Thursday, 28 July 2016

How to search the word and display the matches in given string using javascript?

Html page:

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Javascript Page:

<script>
function myFunction()
{
    var str = "The rain in SPAIN stays mainly in the plain";
    var res = str.match(/ain/g);
    document.getElementById("demo").innerHTML = res;
}
</script>

Output:
ain,ain,ain

Note:

G for global search its is key word

No comments:

Post a Comment