javaScript Regular Expression Object

you can use regular expressions in javascript to search for patterns in a string, replace text, and extract substring.

the regExp object contains a regular expression pattern along with flags that identity how to apply the pattern.

syntax:

          var   reg_exp   =  new   regExp("pattern"[,'flags'])

          or 
      
          var   reg_exp   =   /pattern/[flags]

flags
      optional.flags are used to perform case-insensitive and global searches.

  • g  (global search for all occurrences of pattern)
  • i   (ignore case)
  • m (multiline search)

in this example do a case-insensitive search for 'free" in a string:

 var  str  = "free demo class  free video  free tutorial free  thoughts";
                
 var  pattern =  /free/i;
                
 document.write(str.match(pattern));

 document.write('<br/>'+str.match(/free/g));   // global search for 'free'
 
 document.write('<br/>'+str.match(/free/gi));
 
 // i flag use for ignore case search g flag use for global search

regular expressions are used with the regExp method test() and exec() and with the string methods match, replace, search and split.

regular expression object test() method

the test() method executes the search for a match between a regular expression and specified string.returns true or false.

       var    pattern  =  new  regExp(/dot/i);
       
document.write("pattern is matched_____"+

                                                          pattern.test("this is dot operator"));

       var   pattern_1  =  new  regExp(/xyz/i);
       
document.write('<br/>'+"pattern_1 is not matched_____"+

                                                     pattern_1.test("this is dot operator"));       

regular expression exec() method

the exec() executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

if the exec method does not find a match, it returns null.if it finds a match, exec returns an array.

the array returned by the exec() method has three properties,input, index, lastindex.

  • input : the original string
  • index : contains position of the matched substring within the complete searched string
  • lastindex : the index at which to start the next match
              var    pattern   =  new   regExp(/cat/);
              
              var    array     =  pattern.exec("he is a cat fat black cat");
              
document.write("result = "+array);

document.write("<br/>"+"length = "+array.length);

document.write("<br/>"+"index = "+array.index);

document.write("<br/>"+"input = "+array.input);

regular expression compile() method

the compile() method compiles a regular expression into an internal format for faster execution.

syntax:

         regexp.compile(pattern, [flags])

note : the compile method converts pattern into an internal format for faster execution.


the string object provides four methods that utilize regular expressions .they perform similar and in some case more powerful tasks then the regExp object itself.

match() method

the match() method matches a string with a regular expression, and returns an array containing the results of that search.

if the match method does not find a match, it returns null.

 var  str  = "free demo class  free video  free tutorial free  thoughts";
                
 var  pattern =  /free/i;
                
 document.write(str.match(pattern));

 document.write('<br/>'+str.match(/free/g));   // global search for 'free'
 
 document.write('<br/>'+str.match(/free/gi));
 
 // i flag use for ignore case search g flag use for global search

search() method

the search() method searches a string for a specified value, or regular expression, and returns the position of the match.

This method returns -1 if no match is found.

                var   str ="look the park";
                
                var   n   =str.search(/park/i);
                
                document.write(n);

split() method

split() method is used to split a string into an array of substrings, and returns the new array.

syntax:

     var   string_array  =  string.split(pattern, limit)
     var      str  =  "what are you doing";
     
     var      arr  =str.split(" ");
     
     for( i   in  arr){
     
                         document.write(arr[i]+"<br/>");
                         
                         }