JavaScript regex exec() method for text match.

JavaScript regex exec() method for text match.

JavaScript regex exec() method for text match.

View Answers

May 12, 2012 at 1:50 PM

<html>
<head>
<title> regex exec() for text matching</title>
<script type="text/javascript">
    function validate() {
        var name = document.getElementById("name").value;
        var pattern = /Hello/;
        if (pattern.exec(name)) {
            alert("Pattern matched");
            return true;
        } 
            alert("Pattern is not matched in inputed String");
            return false;
        }
</script>

</head>
<body>
<h2>Matching pattern using exec()..</h2>
Enter any String :
<input type="text" name="name" id="name" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

May 12, 2012 at 1:50 PM

exec() method searches content that matches to the pattern. If it finds the pattern return

array of list otherwise it return null. You can use g modifier for global match as /Hello/g ,

i is used for case insensitive matching and m is used for multiline matching.









Related Tutorials/Questions & Answers:

Ads