/

HTML / CSS / JavaScript Tutorial

JavaScript Tutorial: Regular Expressions

[this page | pdf | back links]

Some JavaScript string methods and properties involve ‘regular expressions’. These take the form:

 

/pattern/modifiers

 

e.g.:

 

var x = /nematrian/i;

 

Modifiers can be i, g or m (or a combination). These have the following interpretations:

 

Modifier

Description

More

g

Do global match (i.e. find all matches rather than just first one)

Here

i

Do case-insensitive match

Here

m

Do a multiline match

Here

 

Regular expressions can include brackets to accept (or reject) a range of characters:

 

Expression

Description

More

[xyz]

Find any character within bracket

Here

[^xyz]

Find any character not within bracket

Here

[0-9]

Find any character within a range, here any digit

Here

[^0-9]

Find any character not within a range, here any digit

Here

[a|b|cd]

Find any from a set of specific alternatives

Here

 

Some characters appearing in regular expressions have special meanings:

 

Expression

Description

More

.

A single character, other than a new line or line terminator

Here

\0

A NUL character

Here

\xxx

The character specified by given octal number xxx (where each x is a decimal digit)

Here

\b

Match at beginning/end of word

Here

\B

Match not at beginning/end of word

Here

\d

A digit

Here

\D

A non-digit

Here

\f

A form feed

Here

\n

A new line

Here

\r

A carriage return

Here

\s

A whitespace

Here

\S

A non-whitespace

Here

\t

A tab

Here

\uhhhh

The character specified by (Unicode) hexadecimal number hhhh (where each h is a hexadecimal digit)

Here

\v

A vertical tab

Here

\w

A word character

Here

\W

A non-word character

Here

\xhh

Character specified by (Ascii) hexadecimal number hh, where each h is a hexadecimal digit)

Here

 

Regular expressions can also have quantifiers with the following meanings, where s is a specified string:

 

Quantifier

Description

More

s+

String that contains at least one s

Here

s*

String that contains zero or more occurrences of s

Here

s?

String that contains zero or one occurrences of s

Here

s{n}

String that contains a sequence of n s’s

Here

s{n1,n2}

String that contains a sequence of n1 to n2 s’s

Here

s{n,}

String that contains a sequence of at least n s’s

Here

s$

String that has s at the end

Here

^s

String that has s at the start

Here

?=s

String that is followed by s

Here

?!s

String that is not followed by s

Here

 

Regular expressions support the following properties and methods:

 

Properties:

 

Property

Description

More

constructor

Returns object’s constructor function

Here

global

Indicates if the “g” modifier is set

Here

ignoreCase

Indicates if the “i” modifier is set

Here

lastIndex

Indicates the index at which to start the next match

Here

multiline

Indicates if the “m” modifier is set

Here

source

Returns the text of the regular expression pattern

Here

 

Methods:

 

Method

Description

More

exec()

Seeks a match in a string and returns the first match

Here

test()

Seeks a match in a string and returns true if found, otherwise false

Here

toString()

Returns the (string) value of a regular expression

Here

 

The compile() method is depreciated and it is therefore recommended that it is not used.

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Regular Expressions


Desktop view | Switch to Mobile