Mega Code Archive

 
Categories / JavaScript Tutorial / Regular Expressions
 

RegExp()

Syntax var variable = new RegExp(pattern, flags) The RegExp() object represents a regular expression that is used for pattern matching. The creation of the object takes pattern and flags parameters. The pattern is a valid regular expression. The flags are either or both g (global) and i (ignore case). Properties and Methods of the RegExp() Object Property/MethodDescription RegExp.$*Represents multiline RegExp.$&Represents lastmatch RegExp.$_Represents input RegExp.$`Represents leftContext RegExp.$'Represents rightContext RegExp.$+Represents lastParen RegExp.$1,$2,...$9Represents substring of matches compile()Compiles a regular expression exec()Executes the search for a match in a specified string global_Specifies whether to check the expressions against all possible matches ignoreCaseWhether case is ignored or not during a string search inputString that is matched lastIndex_Specifies the index at which to start matching the next string. lastMatchLast matched characters lastParenThe last parenthesized substring match leftContextThe substring preceding the most recent match multilineSpecifies whether to search on multiple lines rightContextThe substring following the most recent match sourceThe string pattern test()Tests for a string match <html>     <body>     <script language="JavaScript">     <!--     function isSSN(str){        // xxx-xx-xxxx        var regexp = /^(\d{9}|\d{3}-\d{2}-\d{4})$/;        return regexp.test(str);     }     function checkInput(){       var valid = true;       var ssn = document.form1.ssn.value;       if (!isSSN (ssn)){           window.alert("Invalid SSN: " + ssn);           valid = false;       }       else{         alert(ssn + " is a valid SSN");       }     }     -->     </script>     <form name="form1">     Enter your SSN:     <input type="text" size="15" name="ssn">     <br><br>     <input type="button" value="Validate SSN" onClick='checkInput()'>     <br>     </form>     </body>     </html>