User Guide and Reference

Regular Expressions

Hide Navigation Pane

Regular Expressions

Previous topic Next topic No directory for this topic  

Regular Expressions

Previous topic Next topic Topic directory requires JavaScript JavaScript is required for the print function  

Overview

A regular expression is a sophisticated, industry-standard way of specifying a validation pattern. An explanation of regular expressions is beyond the scope of this document, but there are numerous books and web sites that you can refer to. You can also use search engines to find standard regular expressions for common types of data. An example of some regular expression patterns are:
 

^[a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,4}$
 the pattern for email addresses 
 

^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
the pattern for HTTP:// URLs

 

RegEx References

https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

http://www.regular-expressions.info/javascript.html

http://regex101.com

http://regexpal.com

A good resource for regular expressions is: http://regexlib.com/.

 

Online regular expression editor and evaluator can be found at: http://gskinner.com/RegExr/ and http://www.debuggex.com/.

 

How to Use Regular Expressions

There are two ways to use patterns in JavaScript:

 

var patt = new RegExp(pattern,modifiers);

or more simply:

var patt = /pattern/modifiers;

 

The modifiers (also called "flags" in the documentation) are:

 

g
global match

i
ignore case

m
multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)

y
sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property.

 

In other words, in most cases where you are not using a modifier, just use the pattern as follows:

 

/^[a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,4}$/

 

in your JavaScript code.