Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript RegExp Object

JavaScript RegExp Object


RegExp is short for regular expression.


Complete RegExp object reference

Please see our JavaScript RegExp object reference, which provides all properties and methods that can be used with string objects.

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


What is RegExp?

A regular expression describes a pattern of characters.

When you search through text, you can use a pattern to describe what you are looking for. RegExp is such a pattern.

A simple pattern can be a single character.

A more complex pattern includes more characters and can be used for parsing, format checking, replacement, and so on.

You can specify the search position within a string, the type of characters to search for, and so on.

Syntax

var patt=new RegExp(pattern,modifiers);

Or a simpler method

var patt=/pattern/modifiers;

Note:When creating a regular expression object with the constructor, the usual character escaping rules apply (add a backslash \ in front). For example, the following are equivalent:

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


RegExp modifiers

Modifiers are used to perform case-insensitive and global searches.

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

g - modifier is used to perform a global search (instead of stopping at the first match, it finds all matches).

Example 1

Find "runoob" in a string, case-insensitively

var

str

=

"

Visit RUnoob

"

;

var

patt1

= /

runoob

/

i

;

The text marked below is the expression that obtains the match:

Visit

RUnoob

Try it »

Example 2

Global search for "is"

var

str

=

"

Is this all there is?

"

;

var

patt1

=/

is

/

g

;

The text marked below is the expression that obtains the match:

Is th

is

all there

is

?

Try it »

Example 3

Global and case-insensitive search for "is"

var

str

=

"

Is this all there is?

"

;

var

patt1

=/

is

/

gi

;

The text marked below is the expression that was matched:

Is

th

is

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 a string for the character "e":

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 code above will be:

true

Try it »

When creating a RegExp object with the constructor, normal character escaping rules apply (add a backslash \ in front).

Example

var

re

=

new

RegExp

(

"

\

w+

"

)

;

Try it »


exec()

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

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

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 code above will be:

e

Try it »