Regular Expressions in Java Script
One you have the Regular Expression syntax down working with it inside of Java Script is a breeze.
Inside of Java Script Regular Expression can be created in two ways. As literals or by calling the constructor of the RegExp object. In most cases I prefer calling the constructor, because I find it makes the code more readable.
As a literal:
var re = //bjeff/b/;
Calling the constructor:
var re = new RegExp(“/bjeff/b”);
Perform a search on a string, and then write the results to the page.
var printArray = function (x) {document.writeln(x + "<br>");}
var stringToSearch = "dood this is soo cool";
var re = new RegExp ("d(o+)d", "g"); // return any o’s that are between d and the full word
var matches = re.exec(stringToSearch);
matches.forEach(printArray);
Perform a string substitution, and then write the results to the page
var stringToSearch ="dood this is soo cool, doooooood";
var re = new RegExp("/dood/g");
var processedString = stringToSearch.replace(/dood/g, "dude"); // replace the full word dood w/ dude
document.writeln(processedString);
The Java Script RegExp object has the following methods that are useful.
| exec |
A RegExp method that executes a search for a match in a string. It returns an array of information. |
| test |
A RegExp method that tests for a match in a string. It returns true or false. |
| match |
A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch. |
| search |
A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails. |
| replace |
A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. |
| split |
A String method that uses a regular expression or a fixed string to break a string into an array of substrings. |
Tip #1 Use Regular Expressions For Client Side Validation
Wire your onBlur or keyPress event to call a function that runs a Regular Expression on the content of the control. Below you will find a bunch Regular Expressions for common validation needs.
| Date Format (m/d/y) |
^([\d]|1[0,1,2])/([0-9]|[0,1,2][0-9]|3[0,1])/\d{4}$ |
01/21/2008 |
| Decimal Number |
^\d*[0-9](\.\d*[0-9])?$ |
178.685 |
| Document Filenames |
^[a-zA-Z0-9-_\.]+\.(pdf|txt|doc|csv)$ |
jeff.txt |
| E-mail Address |
^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$ |
jeff@mcwherter.net |
| HTML Color Codes |
^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$ |
#8C9EAA |
| Image Filenames |
^[a-zA-Z0-9-_\.]+\.(jpg|gif|png)$ |
robot.png |
| IP Address |
^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$ |
192.168.0.1 |
| MySQL Date Format |
^\d{4}-(0[0-9]|1[0,1,2])-([0,1,2][0-9]|3[0,1])$ |
2008-01-04 |
| Postal Code |
^([A-Z][0-9]){3}$ |
V2B2S3 |
| Time Format (HH:MM) |
^([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}$ |
12:56 |
| URL |
^(http[s]?://|ftp://)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$ |
http://www.mcwherter.net |
| Phone Number |
^(([0-9]{1})*[- .(]*([0-9a-zA-Z]{3})*[- .)]*[0-9a-zA-Z]{3}[- .]*[0-9a-zA-Z]{4})+$ |
1.517.455.7837 |
Tip # 2 Tools For Regular Expressions in Java Script