Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript RegExp Object

JS RegExp Object

In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.

					 
        
          // literal notation
          const re1 = /ab+c/i;

          // constructor with string pattern as first argument
          const re2 = new RegExp("ab+c", "i");

          // constructor with regular expression literal as first argument
          const re3 = new RegExp(/ab+c/, "i"); 
        
      
RegExp Object Methods

RegExp Object have the following methods.

Method Description
compile() Compiles or re-compiles a regular expression (Deprecated)
exec() Tests for a match in a string. Returns the first match
test() Tests for a match in a string. Returns true or false
toString() Returns the string value of the regular expression
RegExp Object Properties

RegExp Object have the following properties.

Property Description
constructor Returns the function that created the RegExp object's prototype
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
dotAll Checks whether the "s" modifier is set
flags Returns the flags or modifiers of the RegExp pattern
hasIndices Checks whether the "d" modifier is set
sticky Checks whether the "y" modifier is set
unicode Checks whether the "u" modifier is set
unicodeSets Checks whether the "v" modifier is set
The compile() Method

compile() method is used to compile or recompile a regular expression with new source and flags after the RegExp object has already been created.

compile() method is deprecated and avoid using it.

					 
        
          const re = new RegExp("foo", "gi");
          re.compile("new foo", "g"); 
        
      
The exec() Method

exec() method tests for a match in a string. If it finds a match, it returns a result array, otherwise it returns null.

					 
        
          var str = "Javascript is an interesting scripting language";
          var re1 = new RegExp("script", "g");
          var re2 = new RegExp("pushing", "g");
          re1.exec(str); // script
          re2.exec(str); // null
        
      
The test() Method

test() method tests for a match in a string. If it finds a match, it returns true, otherwise it returns false.

					 
        
          var str = "Javascript is an interesting scripting language";
          var re1 = new RegExp("script", "g");
          var re2 = new RegExp("pushing", "g");
          re1.test(str); // true
          re2.test(str); // false
        
      
The toString() Method

toString() method returns the string value of the regular expression.

					 
        
          var re1 = new RegExp("script", "g");
          re1.toString(); // /script/g

          var re2 = new RegExp("/", "g");
          re2.toString(); // /\//g
        
      
The constructor Property

constructor property returns the function that created the RegExp prototype.

					 
        
          var re = new RegExp("script", "g");
          re.constructor; // function RegExp() { [native code] } 
        
      
The global Property

global property specifies whether or not the "g" modifier is set. This property returns true if the "g" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "g");
          re1.global; // true

          var re2 = new RegExp("script");
          re2.global; // false
        
      
The ignoreCase Property

ignoreCase property specifies whether or not the "i" modifier is set. This property returns true if the "i" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "i");
          re1.ignoreCase; // true

          var re2 = new RegExp("script");
          re2.ignoreCase; // false
        
      
The lastIndex Property

lastIndex property specifies the index at which to start the next match. This property only works if the "g" modifier is set.

This property returns an integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods.

exec( ) and test( ) reset lastIndex to 0 if they do not get a match.

					 
        
          var str = "Javascript is an interesting scripting language";
          var re = new RegExp( "script", "g" );
          re.lastIndex; // 0
          re.test(str);
          re.lastIndex; // 10
          re.test(str);
          re.lastIndex; // 35
          re.test(str);
          re.lastIndex; // 0
        
      
The multiline Property

multiline property specifies whether or not the m modifier is set. This property returns true if the "m" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "m");
          re1.multiline; // true

          var re2 = new RegExp("script");
          re2.multiline; // false
        
      
The source Property

source property returns the text of the RegExp pattern.

					 
        
          var re = new RegExp("script", "m");
          re.source; // script
        
      
The dotAll Property

dotAll property specifies whether or not the s modifier is set. This property returns true if the "s" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "s");
          re1.dotAll; // true

          var re2 = new RegExp("script");
          re2.dotAll; // false
        
      
The flags Property

flags property returns the flags of this regular

					 
        
          var re1 = new RegExp("script", "g");
          re1.flags; // g

          var re2 = new RegExp("script", "yum");
          re2.flags; // muy

          var re3 = new RegExp("script");
          re3.flags; // 
        
      
The hasIndices Property

hasIndices property specifies whether or not the d modifier is set. This property returns true if the "d" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "d");
          re1.hasIndices; // true

          var re2 = new RegExp("script");
          re2.hasIndices; // false
        
      
The sticky Property

sticky property specifies whether or not the y modifier is set. This property returns true if the "y" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "y");
          re1.sticky; // true

          var re2 = new RegExp("script");
          re2.sticky; // false
        
      
The unicode Property

unicode property specifies whether or not the u modifier is set. This property returns true if the "u" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "u");
          re1.unicode; // true

          var re2 = new RegExp("script");
          re2.unicode; // false
        
      
The unicodeSets Property

unicodeSets property specifies whether or not the v modifier is set. This property returns true if the "v" modifier is set, otherwise it returns false.

					 
        
          var re1 = new RegExp("script", "v");
          re1.unicodeSets; // true

          var re2 = new RegExp("script");
          re2.unicodeSets; // false
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved