JavaScript > String Methods

String match in JavaScript

How to search for a match in a string in JavaScript?


match()

match is an string method {match() method} in JavaScript, which is used to retrive the matches when matching a string against regular expression. This method having one required parameter, i.e "RegularExpression". 

In JavaScript, by using match() method we can search for a match in a string. The match() method searches against a Regular expressions and returns the matches as an Array object. If no match is found in the string it will return null.

The Array object is used to store the multiple values in a single variable.

NOTE: Regular expression must and should include the g modifier, if not match() method will return only the first match in the string. In case if match is not found it returns NULL.

Searching a match with the global modifier.

    <p>Click the below button to search for a Match.</p>
    <input type="button" value="Match" onclick="Function()" />
    <p id="myId"></p>

    <script>
        function Function() {
            var a = "DotNetFunda was introduced in 2007, TechFunda in 2015, ITFUNDA in 2011, myFunda.net is a part of ITFUNDA";
            var r = a.match(/Fun/g);
            document.getElementById("myId").innerHTML = r;
        }
    </script>

In the above code snippet we have given Id as "myId" to the second <p> element, there is a string with the variable a in the <script> code. We are searching for the value "Fun" in a string by using match() method, for that we used .match(/Fun/g), in that performs the global search. Onclick of the button "Matchin the HTML code fires the Function() in the <script> code at the same time string method match() starts serching against a regular expression for the match in a string and returns the match as an Array object.

OUTPUT

NOTE: In the above output we can notice that the match() method with global modifier retruns only the mentioned formatted ("Fun") elements from string.

Searching a match with the global and case-insensitive modifier.

<p>Click the below button to search for a Match.</p>
<input type="button" value="Match" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "DotNetFunda was introduced in 2007, TechFunda in 2015, ITFUNDA in 2011, myFunda.net is a part of ITFUNDA";
        var r = a.match(/Fun/gi);
        document.getElementById("myId").innerHTML = r;
    }
</script>

Searching with g & i modifiers

We need to search in a string with the global modifier and case-insensitive modifier, for that we are using match(/Fun/gi) in the <script> block. Onclick of the button fires the function in the <script> block, at the same time match(/Fun/gi) method searches a string for the element "Fun/fun" and returns the output.

OUTPUT

If search is not found.

<p>Click the below button to search for a Match.</p>
<input type="button" value="Match" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "DotNetFunda was introduced in 2007, TechFunda in 2015, ITFUNDA in 2011, myFunda.net is a part of ITFUNDA";
        var r = a.match(/can/g);
        document.getElementById("myId").innerHTML = r;
    }
</script>

If Search is not found

In case if search is not found in a string, it returns NULL.

OUTPUT

 Views: 5534 | Post Order: 74



Write for us






Hosting Recommendations