javaScript regular expression

A regular expression describes one or more strings to match when you search a body of text.The regular expression serves as a character pattern to compare with the text that is being searched.

you construct a regular expression in one of two ways.

  • using a regular expression literal, as follows:
          var   pattern  =  /expression/ 
  • calling the constructor function of the regExp object:
          var   pattern  =  new  regExp('ab+c');

some example of regular expression pattern:

                var  pat_1   = /a.b/;
                
                var  pat_2   = /.[hh]o/;
                
                var  pat_3   =/[a-z-a-z-0-9]/;
                
                var  pat_4   =/[a-z-a-z-0-9]+/;

javascript - how create the regular expression pattern

character classes : the notation [] can be used to define a pattern that represents a set of characters, called a character class.

characters enclosed in a bracket expression match only a single character for the position in the regular expression where the bracket expression appears.

      var  pattern    = /[xyz]/g;                           	//g flag for global search

the - character is used to specify intervals inside the [] notation.the following regular expression is equivalent to /number[12345]/.

      var  pattern    = /number[1-5]/g ;

You can also find all characters that are not in a list or range by including the caret (^) character at the start of the list[]

If the caret(^) character appears in any other position in the list, it matches itself; that is, it has no special meaning.

      var  pattern    = /number[^1-5]/g ;

note: the metacharacters $, ., ?, +, (, ) and | are have not special meaning in a [] construct.

patternmatchestry it
[abc]a, b, c 
[^abc]any character except a, b, or c 
[a-za-z]a through z or a through z, inclusive (range) 

matching any character

the period(.) matches any single printing or non-printing character in a string except a newline character(/n).

the regexp /a.c/ in javaScript matches 'abc', 'aac', 'adc', 'acc', 'a1c', 'a2c', 'a#c'.

The following table contains a list of multiple-character metacharacters and their behavior in regular expressions.

metacharactermatchestry it
.any character except newline 
\wany word character. same as [a-za-z0-9] 
\Wany non-word character. same as [^a-za-z0-9]
\sany whitespace character. same as [\t\n\r\f\v] 
\sany non-whitespace character. same as [^ \t\n\r\f\v] 
\dany digit. same as [0-9] 
\dany non-digit. same as [^0-9]
\bmatches a word boundary, that is the position between a word and a space. 
\bmatched a word non-boundary 

boundary matches

the boundary matchers(anchors ^/$) can be used to finding a pattern match at either the beginning or the end of a string/line.

     var  str = "#what is your name ?";
     
     var  pattern   = /?$/;
     
     var  pattern_1 = /^#/;
     
     document.write("match end of string____"+str.match(pattern)); 
     
     document.write("match start of string____"+str.match(pattern_1));

quantifiers

quantifiers are powerful operators that repeatedly try to match a regular expression with the remaining characters in the input.

  • r?, that matches the regular expression r zero or one time.
  • r*, that matches the regular expression r zero or more times.
  • r+, that matches the regular expression r one or more time.
     var  str = "what is aa that so are ss ssss aaaa is good";
     
     var  firstpattern   = /a*a/g;
     
     var  secondpattern = /s+/g;
     
     document.write("match zero or more time____"+str.match(firstpattern)); 
     
document.write('<br/>'+"match one or more time____"+str.match(secondpattern));