JavaScript: Advanced Guide to Programming Code with JavaScript (JavaScript, Java, Python, Programming, Code, Project Management, Computer Programming Book 4) by Charlie Masterson
Chapter 10:
Regular Expressions – Methods
The JavaScript regular expression method contains two main methods for string testing: test() and exec().
Theexec() Method
This method takes an argument and a string and checks to see if there are one or more matches in the string to a pattern that is specified in the regular expressions. If the matches are found, a result array is returned by the method showing the starting point of each match. If there are no matches, null is returned.
Thetest() Method
This method will also take a string and an argument and check the string for pattern matches. If there is a match, true will be returned; if not, false is returned. This is incredibly useful in form validations scripts.
The following example show how to use this to test a social security number. Ignore the regular expression syntax for now, we will be looking at that in the next chapter:
Example:
RegularExpressions/Demos/SsnChecker.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>ssn Checker</title>
<script type="text/javascript">
var reSSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;