JavaScript RegExp

By:    Updated: January 24,2017

JavaScript supports regular expressions through the RegExp type. Regular expressions are easy to create using syntax similar to Perl. There are two ways to create regular expressions.

//Method1: Using regular expression literals
var expression = /pattern/flags;

//Method2: Using new RegExp constructor
var expression = new RegExp(pattern, flags);

The pattern part of the expression can be any simple or complicated regular expression, including character classes, quantifiers, grouping, lookaheads, and backreferences. Each expression can have zero or more flags indicating how the expression should behave. ECMAScript 5 supports  three flags represent matching modes:

  g   Global mode, means that the pattern will be applied to all of the string instead of stopping after the first match is found.
  i   Case-insensitive mode, means that the case of the pattern and the string are ignored when determining matches.
  m   Multiline mode, means that the pattern will continue looking for matches after reaching the end of one line of text.
<script type="text/javascript">
var site_name = "valinv";

//match the first instances of "valinv" in a string, regardless of case.
var pattern1 = /valinv/i;

//match all instances of "valinv" in a string.
var pattern2 = /valinv/g;

//match the first instances of valinv or value, regardless of case.
var pattern3 = /val(inv|ue)/i;

//match the first instances of string begining with val, regardless of case.
var pattern4 = /val.*/i;
</script>

As with regular expressions in other languages, all metacharacters must be escaped when used as part of the pattern. The metacharacters are as follows:

() {} [] ^ $ \ | . * ? +

Each metacharacter has one or more uses in regular-expression syntax and so must be escaped by a backslash when you want to match the character in a string.

<script type="text/javascript">
var site_domain = "valinv.com";

//match the first instance of "valinv.com", regardless of case.
var pattern = /valinv\.com/i;
</script>

RegExp Instance Methods

exec(), which is intended for use with capturing groups. This method accepts a single argument, which is the string on which to apply the pattern, and returns an array of information about the first match or null if no match was found.In the array, the first item is the string that matches the entire pattern, the second item is the first capturing groups, the third item is the second capturing groups and so forth.

//from left to right
(1st (2nd    (3rd  (4th (.))  )   ) )
<script type="text/javascript">
var site_domain = "www.valinv.com";

var pattern = new RegExp("((.*)\.(valinv)\.com)", "i");

var matches = pattern.exec(site_domain);

//matches the entire pattern
alert(matches[0]); //www.valinv.com

//the 1st capturing groups
alert(matches[1]); //www.valinv.com

//the 2nd capturing groups
alert(matches[2]); //www

//the 3rd capturing groups
alert(matches[3]); //valinv
</script>

Another method of regular expressions is test(), which accepts a string argument and returns true if the pattern matches the argument and false if it does not. This method is useful when you want to know if a pattern is matched, but you have no need for the actual matched text.

<script type="text/javascript">
var site_domain = "www.valinv.com";

var pattern = new RegExp(".*\.valinv\.com", "i");

var check = pattern.test(site_domain);

if(check)
{
	alert("This is a subdomain of valinv.com");
}
else
{
	alert("This isn't a subdomain of valinv.com");
}
</script>
More in Development Center
New on Valinv
Sources
  • http://www.regular-expressions.info
Related Articles
Sponsored Links