JavaScript RegExp Object


RegExp: is a shorthand for regular expression.


Complete RegExp object references

Check out our reference manual for JavaScript RegExp objects, which provides all the properties and methods that you can use with string objects.

This manual contains detailed descriptions and examples of the usage of each property and method.


What is RegExp?

Regular expressions describe pattern objects for characters.

When you retrieve a piece of text, you can use a pattern to describe what you want to retrieve. RegExp is such a model.

A simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, formatting, substitution, and so on.

You can specify where to retrieve in a string, the type of character to retrieve, and so on.

Grammar

var patt=new RegExp(pattern,modifiers);

Or a simpler way

var patt=/pattern/modifiers;

  • a pattern describes an expression model.

  • modifiers describe whether the retrieval is global, case sensitive, etc.

Note: when using constructors to create regular objects, the general character escape rules (preceded by a backslash \) are required. for example, the following is equivalent:

var re = new RegExp("\\w+");
var re = /\w+/;

RegExp modifier

modifiers are used to perform case-insensitive and full-text searches.

i - the modifier is used to perform case-insensitive matching.

g - the modifier is used to perform a full-text search (instead of stopping the lookup when the first one is found, all matches are found).

Example 1

find "tutorialfish" in strings without case sensitivity

var str = "Visit TUtorialFish";
var patt1 = /tutorialfish/i;

the following tag of the text is the resulting matching expression:

Visit TUtorialfish

Try It! 

Example 2

full-text lookup "is"

var str="Is this all there is?";
var patt1=/is/g;

the following tag of the text is the resulting matching expression:

Is this all there is?

Try It! 

Example 3

full-text lookup and case-insensitive search "is"

var str="Is this all there is?";
var patt1=/is/gi;

the following tag of the text is the resulting matching expression:

Is this all there is?

Try It! 


test()

the test() method searches for the specified value of the string, based on the result, and returns true or false.

the following example searches for the character "e" from a string:

Example

var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

Due to the presence of the letter "e" in the string, the output of the above code will be:

true

Try It! 

when using constructors to create regular objects, general character escape rules are required (preceded by a backslash \)

Example

var re = new RegExp("\\w+");

Try It! 


exec()

the exec() method retrieves the specified value in the string. the return value is the value that was found. if no match is found, null is returned.

the following example searches for the character "e" from a string:

Example 1

var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));

due to the presence of the letter "e" in the string, the output of the above code will be:

e

Try It!