w3schools
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About

JavaScript RegExp Object

« Previous Next Chapter »

RegExp Object

A regular expression is an object that describes a pattern of characters.

Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on text.

Syntax

var txt=new RegExp(pattern,modifiers);

or more simply:

var txt=/pattern/modifiers;
  • pattern specifies the pattern of an expression
  • modifiers specify if a search should be global, case-sensitive, etc.

Modifiers

F: Firefox, IE: Internet Explorer

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

Modifier Description F IE
i Perform case-insensitive matching 1 4
g Perform a global match (find all matches rather than stopping after the first match) 1 4
m Perform multiline matching 1 4

Brackets

Brackets are used to find a range of characters:

Expression Description F IE
[abc] Find any character between the brackets 1 4
[^abc] Find any character not between the brackets 1 4
[0-9] Find any digit from 0 to 9 1 4
[a-z] Find any character from lowercase a to lowercase z 1 4
[A-Z] Find any character from uppercase A to uppercase Z 1 4
[a-Z] Find any character from lowercase a to uppercase Z 1 4
[adgk] Find any character in the given set 1 4
[^adgk] Find any character outside the given set 1 4
[red|blue|green] Find any of the alternatives specified 1 4

Metacharacters

Metacharacters are characters with a special meaning:

Metacharacter Description F IE
. Find a single character, except newline or line terminator 1 4
\w Find a word character 1 4
\W Find a non-word character 1 4
\d Find a digit 1 4
\D Find a non-digit character 1 4
\s Find a whitespace character 1 4
\S Find a non-whitespace character 1 4
\b Find a match at the beginning/end of a word 1 4
\B Find a match not at the beginning/end of a word 1 4
\0 Find a NUL character 1 4
\n Find a new line character 1 4
\f Find a form feed character 1 4
\r Find a carriage return character 1 4
\t Find a tab character 1 4
\v Find a vertical tab character 1 4
\xxx Find the character specified by an octal number xxx 1 4
\xdd Find the character specified by a hexadecimal number dd 1 4
\uxxxx Find the Unicode character specified by a hexadecimal number xxxx 1 4

Quantifiers

Quantifier Description F IE
n+ Matches any string that contains at least one n 1 4
n* Matches any string that contains zero or more occurrences of n 1 4
n? Matches any string that contains zero or one occurrences of n 1 4
n{X} Matches any string that contains a sequence of X n's 1 4
n{X,Y} Matches any string that contains a sequence of X or Y n's 1 4
n{X,} Matches any string that contains a sequence of at least X n's 1 4
n$ Matches any string with n at the end of it 1 4
^n Matches any string with n at the beginning of it 1 4
?=n Matches any string that is followed by a specific string n 1 4
?!n Matches any string that is not followed by a specific string n 1 4

RegExp Object Properties

F: Firefox, IE: Internet Explorer

Property Description F IE
global Specifies if the "g" modifier is set 1 4
ignoreCase Specifies if the "i" modifier is set 1 4
lastIndex The index at which to start the next match 1 4
multiline Specifies if the "m" modifier is set 1 4
source The text of the RegExp pattern 1 4

RegExp Object Methods

Method Description F IE
compile() Compiles a regular expression 1 4
exec() Tests for a match in a string. Returns a result array 1 4
test() Tests for a match in a string. Returns true or false 1 4

« Previous Next Chapter »


1,050,724 Sites built with Wix. Make your own!

Click here to design a Stunning Flash Website for Free

Wix is a revolutionary web design tool that provides anyone with the possibility to create professional and beautiful websites for free.

With e-commerce features, search engine visibility and many more professional tools, Wix is the ultimate solution for creating a spectacular site while saving tons of money.


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Top Web Hosting
Windows Hosting
WEB BUILDING
Download XML Editor
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display