JavaScript RegExpObjects


RegExp: is the abbreviation for regular expression.


Complete RegExp Object Reference Manual

Please see ourJavaScript RegExp Object Reference Manual, which provides all properties and methods that can be used 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 of characters.

When you search a text, you can use a pattern to describe the content to be searched. RegExp is such a pattern.

A simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, replacement, etc.

You can specify the search position in a string, and the type of characters to search for, etc.

Syntax

var patt=new RegExp(pattern,modifiers);

or a simpler method

var patt=/pattern/modifiers;
  • The pattern describes an expression model.
  • Modifiers describe whether the search is global, case-sensitive, etc.

Note:When using the constructor to create a regular expression object, the usual character escaping rules are needed (prepend a backslash \). For example, the following are equivalent:

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

RegExp Modifiers

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 at the first match, it finds all matches).

Example 1

Search for "example" in a string without case sensitivity

var str = "Visit example"; var patt1 = /example/i;

The followingmarkedtext is the obtained matching expression:

Visit example

Try it »

Example 2

Full-text search for "is"

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

The followingmarkedtext is the obtained matching expression:

Is this all there is?

Try it »

Example 3

Full-text and case-insensitive search for "is"

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

The followingmarkedtext is the obtained matching expression:

Is this all there is?

Try it »


test()

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

The following example searches for the character "e" in a string:

Example

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

Since the letter "e" exists in the string, the output of the above code will be:

true

Try it »

When using the constructor to create a regular expression object, the usual character escaping rules are needed (prepend a backslash \)

Example

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

Try it »


exec()

The exec() method searches for the specified value in a string. The return value is the value found. If no match is found, it returns null.

The following example searches for the character "e" in a string:

Example 1

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

Since the letter "e" exists in the string, the output of the above code will be:

e

Try it »
Other Extensions