JS - INTRODUCTIE

Koppelingen
Voorbeelden bij dit hoofdstuk

HOOFDSTUKKEN

Algemeen



Algemeen

Terug naar de top


TEKST

JavaScript RegExp Object

previous next


The RegExp object is used to specify what to search for in a text


What is RegExp

RegExp, is short for regular expression.

When you search in a text, you can use a pattern to describe what you are searching for. RegExp IS this pattern.

A simple pattern can be a single character.

A more complicated pattern consists of more characters, and can be used for parsing, format checking, substitution and more.

You can specify where in the string to search, what type of characters to search for, and more.


Defining RegExp

The RegExp object is used to store the search pattern.

We define a RegExp object with the new keyword. The following code line defines a RegExp object called patt1 with the pattern "e":

var patt1=new RegExp("e");

When you use this RegExp object to search in a string, you will find the letter "e".


Methods of the RegExp Object

The RegExp Object has 3 methods: test(), exec(), and compile().


test()

The test() method searches a string for a specified value. Returns true or false

Example:

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

Since there is an "e" in the string, the output of the code above will be:

true

Try it yourself


exec()

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

Example 1:

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

Since there is an "e" in the string, the output of the code above will be:

e

Try it yourself

Example 2:

You can add a second parameter to the RegExp object, to specify your search. For example; if you want to find all occurrences of a character, you can use the "g" parameter ("global").

For a complete list of how to modify your search, visit our complete RegExp object reference.

When using the "g" parameter, the exec() method works like this:

  • Finds the first occurence of "e", and stores its position
  • If you run exec() again, it starts at the stored position, and finds the next occurence of "e", and stores its position
var patt1=new RegExp("e","g");
do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)

Since there is six "e" letters in the string, the output of the code above will be:

eeeeeenull

Try it yourself


compile()

The compile() method is used to change the RegExp.

compile() can change both the search pattern, and add or remove the second parameter.

Example:

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

Since there is an "e" in the string, but not a "d", the output of the code above will be:

truefalse

Try it yourself


JavaScript RegExp Object Reference


RegExp Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

MethodDescriptionFFNIE
compile Change the regular expression (what to search for) 1 4 4
exec Search a string for a specified value. Returns the found value and remembers the position 1 4 4
test Search a string for a specified value. Returns true or false 1 4 4

String Object Methods that supports Regular Expressions

MethodDescriptionFFNIE
search Search a string for a specified value. Returns the position of the value 1 4 4
match Search a string for a specified value. Returns an array of the found value(s) 1 4 4
replace Replace characters with other characters 1 4 4
split Split a string into an array of strings 1 4 4

 


RegExp Modifiers

PropertyDescriptionFFNIE
Pattern flags
i Ignore the case of characters 1 4 4
g Global search. Search all occurrences of the regular expression in a string 1 4 4
gi Global search, ignore case 1 4 4
m Multiline mode. Matches occurrences over multiple lines 1 4 4
Position Matching
^ Get a match at the beginning of a string 1 4 4
$ Get a match at the end of a string 1 4 4
\b Word boundary. Get a match at the beginning or end of a word in the string 1 4 4
\B Non-word boundary. Get a match when it is not at the beginning or end of a word in the string 1 4 4
?= A positive look ahead. Get a match if a string is followed by a specific string 1 4 4
?! A negative look ahead. Get a match if a string is not followed by a specific string 1 4 4
Literals
\0 Find a NULL character 1 4 4
\n Find a new line character 1 4 4
\f Find a form feed character 1 4 4
\r Find a carriage return character 1 4 4
\t Find a tab character 1 4 4
\v Find a vertical tab character 1 4 4
\xxx Find the ASCII character expressed by the octal number xxx 1 4 4
\xdd Find the ASCII character expressed by the hex number dd 1 4 4
\uxxxx Find the ASCII character expressed by the UNICODE xxxx 1 4 4
Character Classes
[xyz] Find any character in the specified character set 1 4 4
[^xyz] Find any character not in the specified character set 1 4 4
. (dot) Find any character except newline or line terminator 1 4 4
\w Find any alphanumeric character including the underscore 1 4 4
\W Find any non-word character 1 4 4
\d Find any single digit 1 4 4
\D Find any non-digit 1 4 4
\s Find any single space character 1 4 4
\S Find any single non-space character 1 4 4
Repetition
{x} Finds the exact (x) number of the regular expression grouped together 1 4 4
{x,} Finds the exact (x) or more number of the regular expression grouped together 1 4 4
{x,y} Finds between x and y number of the regular expression grouped together 1 4 4
? Finds zero or one occurrence of the regular expression 1 4 4
* Finds zero or more occurrences of the regular expression 1 4 4
+ Finds one or more occurrences of the regular expression 1 4 4
Grouping
( ) Finds the group of characters inside the parentheses and stores the matched string 1 4 4
(?: ) Finds the group of characters inside the parentheses but does not store the matched string 1 4 4
| Combines clauses into one regular expression and then matches any of the individual clauses. Similar to "OR" statement 1 4 4
Back references
( )\n Back reference. Uses the stored matched string. i.e. from the ( ) modifier 1 4 4

RegExp Object Properties

PropertyDescriptionFFNIE
global Specifies if the "g" modifier is set 1 4 4
ignoreCase Specifies if the "i" modifier is set 1 4 4
input The string on which the pattern match is performed 1 4 4
lastIndex An integer specifying the index at which to start the next match 1 4 4
lastMatch The last matched characters 1 4 4
lastParen The last matched parenthesized substring 1 4 4
leftContext The substring in front of the characters most recently matched 1 4 4
multiline Specifies if the "m" modifier is set 1 4 4
prototype Allows you to add properties and methods to the object 1 4 4
rightContext The substring after the characters most recently matched 1 4 4
source The text used for pattern matching 1 4 4

 


**************************

JavaScript compile() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The compile() method is used to change the regular expression.

Syntax

RegExpObject.compile(regexp)

 

ParameterDescription
regexp Required. The new regular expression to search for

 


Example

In the following example we change the pattern to search for from "Microsoft" to "W3Schools":

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("Microsoft");
document.write(patt1.test(str) + ". Match not found <br />");
patt1.compile("W3Schools");
document.write(patt1.test(str) + ". Match found <br />");
</script>

 


***********************************

JavaScript exec() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The exec() method is used to search for a match of a regular expression in a string.

This method returns the matched text if a match is found, and null if not.

Syntax

RegExpObject.exec(string)

 

ParameterDescription
RegExpObject Required. The regular expression to use
string Required. The string to search

Example

In the following example we will search for "W3Schools":

<script type="text/javascript">
var str = "Visit W3Schools"; 
var patt = new RegExp("W3Schools");

var result = patt.exec(str);
document.write(result);
</script>

 


***********************************

JavaScript test() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The test() method is used to search for a match of a regular expression in a string.

This method returns true if a match is found, and false if not.

Syntax

RegExpObject.test(string)

 

ParameterDescription
RegExpObject Required. The regular expression to use
string Required. The string to search

 


Example

In the following example we will search for "W3Schools":

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("W3Schools");
var result = patt1.test(str);
document.write("Result: " + result);
</script>

 


*********************************

JavaScript search() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The search() method is used to search a string for a specified value.

The search() method returns the position of the specified value in the string. If no match was found it returns -1.

This method supports regular expressions.

Syntax

stringObject.search(regexp)

 

ParameterDescription
regexp Required. A regular expression.

 


Tips and Notes

Note: search() is case sensitive.


Example

In the following example we will search for "W3Schools":

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = /W3Schools/;
var result = str.search(patt1);
document.write("Position: " + result + "<br />");
</script>

 


**********************************

JavaScript match() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The match() method is used to search a string for a specified value.

The match() method returns an array of the matches found. If no match was found it returns null.

This method supports regular expressions.

Syntax

stringObject.match(regexp)

 

ParameterDescription
regexp Required. A regular expression.

 


Tips and Notes

Note: The match() method is case sensitive!


Example

In the following example we will search for "ain":

<script type="text/javascript">
var str = "The rain in Spain stays mainly in the plain"; 
var patt1 = /ain/g;

document.write(str.match(patt1));
</script>

 


**********************************

JavaScript replace() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The replace() method is used to replace some characters in a string with some other characters.

This method supports regular expressions.

Syntax

stringObject.replace(regexp,str)

 

ParameterDescription
regexp Required. The regular expression to find and replace
newstring Required. Specifies the replacement string

 


Tips and Notes

Note: The replace() method is case sensitive.


Example

In the following example we will replace "the" with "Ze":

<script type="text/javascript">
var str = "The Lord of the Rings: The Fellowship of the Ring"; 
var result = str.replace(/The/gi, "Ze");
document.write(result);
</script>

 


********************************

JavaScript split() Method


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The split() method is used to split a string into an array of strings.

This method supports regular expressions.

Syntax

stringObject.split(spl,howmany)

 

ParameterDescription
spl Required. Specifies the character, regular expression, or substring that is used to determine where to split the string
howmany Optional. Specify how many times split should occur. Must be a numeric value

 


Tips and Notes

Note: If an empty string ("") is used as the separator, the string is split between each character.


Example

In this example we will split up a string into an array, using a regular expression to remove any non word characters:

<script type="text/javascript">
var str = "Peter Lois Chris Meg Stewie";
var pattern = /\W+/;
result = str.split(pattern);
document.write(result);
</script>

 


****************************************

JavaScript RegExp i Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the i modifier is set the regular expression ignores the case of the characters.

Syntax

new RegExp("regexp","i")
OR
/regexp/i

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a case insensitive search for "w3schools":

var str = "Visit W3Schools";
var patt1 = new RegExp("w3schools","i");

The marked text below shows where the RegExp gets a match:

Visit W3Schools

 


Try-It-Yourself Demos i How to use the "i" modifier to do a case insensitive search for a sting using a string object. i How to use the "i" modifier to do a case insensitive search for a sting using a regular expression object.

***************************************

JavaScript RegExp g Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the g modifier is set the regular expression finds all occurrences of a regular expression in a string.

Syntax

new RegExp("regexp","g")
OR
/regexp/g

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Tips and Notes

Note: When using the Regular Expression object and the g modifier is set, the lastIndex property is set when executing a search. The lastIndex property specifies the index (placement) in the string where to start the next match.


Example

In the following code we will do a global search for "is":

var str = "Is this all there is?";
var patt1 = new RegExp("is","g");

The marked text below shows where the RegExp gets a match:

Is this all there is?

 


Try-It-Yourself Demos g How to use the "g" modifier to find all the occurrences of the regular expression in a string using a string object. g How to use the "g" modifier to find all the occurrences of the regular expression in a string using a regular expression object.

***************************************

JavaScript RegExp gi Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The gi modifier is used to do a case insensitive search of all occurrences of a regular expression in a string.

Syntax

new RegExp("regexp","gi")
OR
/regexp/gi

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Tips and Notes

Note: When using the Regular Expression object and the g modifier is set, the lastIndex property is set when executing a search. The lastIndex property specifies the index (placement) in the string where to start the next match.


Example

In the following code we will do a global, case insensitive search for "is":

var str = "Is this all there is?";
var patt1 = new RegExp("is","gi");

The marked text below shows where the RegExp gets a match:

Is this all there is?

 


Try-It-Yourself Demos

gi
How to use the "gi" modifier to do a case insensitive search for all the occurrences of the regular expression in a string using a string object.

gi
How to use the "gi" modifier to do a case insensitive search for all the occurrences of the regular expression in a string using a regular expression object.


RegExp Object Reference Complete RegExp Object Reference


****************************************

JavaScript RegExp ^ Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the ^ modifier is set the regular expression gets a match at the beginning of the string.

Syntax

new RegExp("^regexp")
OR
/^regexp/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a search for "Is" at the beginning of the string:

var str = "Is this all there is?";
var patt1 = new RegExp("^Is");

The marked text below shows where the RegExp gets a match:

Is this all there is?

 


Try-It-Yourself Demos

^
How to use the "^" modifier to search in the beginning of a string using a string object.

^
How to use the "^" modifier to search in the beginning of a string using a regular expression object.


RegExp Object Reference Complete RegExp Object Reference


*****************************************************

JavaScript RegExp $ Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the $ modifier is set the regular expression gets a match at the end of the string.

Syntax

new RegExp("regexp$")
OR
/regexp$/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a search for "is" at the end of the string:

var str = "Is this all there is";
var patt1 = new RegExp("is$");

The marked text below shows where the RegExp gets a match:

Is this all there is

 


Try-It-Yourself Demos

$
How to use the "$" modifier to search at the end of a string using a string object.

$
How to use the "$" modifier to search at the end of a string using a regular expression object.


RegExp Object Reference Complete RegExp Object Reference


*******************************************

JavaScript RegExp \b Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \b modifier is set the regular expression gets a match at the beginning or end of a word in the string.

Syntax

/\bregexp/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a search for "W3" at the beginning or end of a word in the string:

var str = "Visit W3Schools";
var patt1 = /\bW3/;
var result = str.match(patt1);
document.write(result);

The marked text below shows where the RegExp gets a match:

Visit W3Schools


********************************************

JavaScript RegExp \B Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \B modifier is set the regular expression gets a match when it is not at the beginning or end of a word in the string.

Syntax

/\Bregexp/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a search for "is" that is not at the beginning or end of a word in the string:

var str = "Visit W3Schools";
var patt1 = /\Bis/;

The marked text below shows where the RegExp gets a match:

Visit W3Schools

 


*************************************

JavaScript RegExp ?= Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the ?= modifier is set the regular expression gets a match if the string to search for is followed by a specific string.

Syntax

new RegExp("regexp(?=ahead)")
OR
/regexp(?=ahead)/

 

ParameterDescription
regexp Required. The rest of the regular expression to use
ahead What the string must be followed by to get a match

 


Example

In the following code we will do a search for "is" if it is ahead of " all":

var str = "Is this all there is";
var patt1 = new RegExp("is(?= all)");

The marked text below shows where the RegExp gets a match:

Is this all there is

 


Try-It-Yourself Demos

?=
How to use the "?=" modifier to search for a string followed by another string using a string object.

?=
How to use the "?=" modifier to search for a string followed by another string using a regular expression object.


RegExp Object Reference Complete RegExp Object Reference


*****************************

JavaScript RegExp ?! Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the ?! modifier is set the regular expression gets a match if the string to search for is not followed by a specific string.

Syntax

new RegExp("regexp(?!ahead)")
OR
/regexp(?!ahead)/

 

ParameterDescription
regexp Required. The rest of the regular expression to use
ahead What the string must not be followed by to get a match

 


Example

In the following code we will do a case insensitive global search for "is" if it is not followed by " all":

var str = "Is this all there is";
var patt1 = new RegExp("is(?= all)","gi");

The marked text below shows where the RegExp gets a match:

Is this all there is

 


Try-It-Yourself Demos

?!
How to use the "?!" modifier to search for a string not followed by another string using a string object.

?!
How to use the "?!" modifier to search for a string not followed by another string using a regular expression object.


RegExp Object Reference Complete RegExp Object Reference


**************************************

JavaScript RegExp \n Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \s modifier is set the regular expression finds newline characters.

Syntax

new RegExp("\n")
OR
/\n/

 


Example

In the following code we will do a search for newline characters:

var str = "Visit W3Schools.\nLearn Javascript.";
var patt1 = new RegExp("\n");

The white spaces in the text below will get a match:

Visit W3Schools.\nLearn Javascript.

 


Try-It-Yourself Demos

/n
How to use the "/n" modifier to find all the occurrences of newline characters using a regular expression object.

/n
How to use the "/n" modifier to find all the occurrences of newline characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


*********************************

JavaScript RegExp \xxx Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \xxx modifier is set the regular expression finds an ASCII character expressed by an octal number

The characters inside the brackets can be any octal number matching an ASCII character.

Syntax

new RegExp("\xxx")
OR
/\xxx/

 


Example

In the following code we will do a search for "W":

var str = "Visit W3Schools";
var patt1 = new RegExp("\127");

The white spaces in the text below will get a match:

Visit W3Schools

 


Try-It-Yourself Demos

/xxx
How to use the "/xxx" modifier to find occurrences of an ASCII character specified by an octal value using a regular expression object.

/xxx
How to use the "/xxx" modifier to find occurrences of an ASCII character specified by an octal value using a string object.


RegExp Object Reference Complete RegExp Object Reference


**************************

JavaScript RegExp \xdd Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \xdd modifier is set the regular expression finds an ASCII character expressed by hex code

The "dd" characters inside the brackets can be any hex code matching an ASCII character.

Syntax

new RegExp("\xdd")
OR
/\xdd/

 


Example

In the following code we will do a search for "W":

var str = "Visit W3Schools";
var patt1 = new RegExp("\x57");

The white spaces in the text below will get a match:

Visit W3Schools

 


Try-It-Yourself Demos

/xdd
How to use the "/xdd" modifier to find occurrences of an ASCII character specified by a hex value using a regular expression object.

/xdd
How to use the "/xdd" modifier to find occurrences of an ASCII character specified by a hex value using a string object.


RegExp Object Reference Complete RegExp Object Reference


*************************

JavaScript RegExp \uxxxx Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \xdd modifier is set the regular expression finds an ASCII character expressed by Unicode

The "xxxx" characters inside the brackets can be any Unicode code matching an ASCII character.

Syntax

new RegExp("\uxxxx")
OR
/\uxxxx/

 


Example

In the following code we will do a search for "W":

var str = "Visit W3Schools";
var patt1 = new RegExp("\u0057");

The white spaces in the text below will get a match:

Visit W3Schools

 


Try-It-Yourself Demos

/uxxxx
How to use the "/uxxxx" modifier to find occurrences of an ASCII character specified by a Unicode value using a regular expression object.

/uxxxx
How to use the "/uxxxx" modifier to find occurrences of an ASCII character specified by a Unicode value using a string object.


RegExp Object Reference Complete RegExp Object Reference


**********************************

JavaScript RegExp [xyz] Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the [xyz] modifier is set the regular expression finds specific characters or span of characters.

The characters inside the brackets can be any characters or span of characters.

Syntax

new RegExp("[xyz]")
OR
/[xyz]/

 


Example

In the following code we will do a global search for the character span "a" to "h":

var str = "Is this all there is?";
var patt1 = new RegExp("[a-h]","g");

The marked text below shows where the RegExp gets a match:

Is this all there is?

 


Try-It-Yourself Demos

charset
How to use the "[a-h]" modifier to find all the occurrences of the character span "a" to "h".


RegExp Object Reference Complete RegExp Object Reference


*************************************

JavaScript RegExp [^xyz] Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the [^xyz] modifier is set the regular expression finds characters that is not the specific characters or span of characters.

The characters inside the brackets can be any characters or span of characters.

Syntax

new RegExp("[^xyz]")
OR
/[^xyz]/

 


Example

In the following code we will do a global search for characters that not in the "a" to "h" span:

var str = "Is this all there is?";
var patt1 = new RegExp("[^a-h]","g");

The marked text below shows where the RegExp gets a match:

Is this all there is?

 


Try-It-Yourself Demos

not charset
How to use the "[^a-h]" modifier to find all occurrences of characters that is not in the characters span "a" to "h".


RegExp Object Reference Complete RegExp Object Reference


**************************

JavaScript RegExp . Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the . modifier is set the regular expression matches the . character to all characters except newline or other line terminators.

Syntax

new RegExp("regexp.")
OR
/regexp./

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a global search for "h.t" (will find "hat"):

var str = "That's that!";
var patt1 = new RegExp("h.t","g");

The marked text below shows where the RegExp gets a match:

That's that!

 


Try-It-Yourself Demos

dot
How to use the "." modifier to find all the occurrences "h.t".


RegExp Object Reference Complete RegExp Object Reference


**********************************

JavaScript RegExp \w Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \w modifier is set the regular expression finds any alphanumeric (a-z, A-Z, 0-9 and _ ) character including underscore.

Syntax

/\w/

 


Example

In the following code we will do a global search for the all alphanumeric characters:

var str = "Give 100%";
var patt1 = /\w/g;

The marked text below shows where the RegExp gets a match:

Give 100%

 


Try-It-Yourself Demos

\w
How to use the "\w" modifier to find all the occurrences of alphanumeric characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


*****************************

JavaScript RegExp \W Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \W modifier is set the regular expression finds any non alphanumeric (not a-z, A-Z, 0-9 or _ ) character.

Syntax

/\W/

 


Example

In the following code we will do a global search for the all non alphanumeric characters:

var str = "Give 100%!!!";
var patt1 = /\W/g;

The marked text below shows where the RegExp gets a match:

Give100%!!!

 


Try-It-Yourself Demos

\W
How to use the "\W" modifier to find all the occurrences of non alphanumeric characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


*****************

JavaScript RegExp \d Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \d modifier is set the regular expression finds a single digit

Syntax

/\d/

 


Example

In the following code we will do a global search for the digits:

var str = "Give 100%";
var patt1 = /\d/g;

The marked text below shows where the RegExp gets a match:

Give 100%

 


Try-It-Yourself Demos

\d
How to use the "\d" modifier to find all the occurrences of single digits using a string object.


RegExp Object Reference Complete RegExp Object Reference


**************************

JavaScript RegExp \D Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \D modifier is set the regular expression finds any non digit character.

Syntax

/\D/

 


Example

In the following code we will do a global search for any character that is not a digit:

var str = "Give 100%";
var patt1 = /\D/g;

The marked text below shows where the RegExp gets a match:

Give 100%

 


Try-It-Yourself Demos

\D
How to use the "\D" modifier to find all the occurrences of non digit characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


**************************

JavaScript RegExp \s Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \s modifier is set the regular expression finds single space characters.

A single space character can be:

  • A whitespace character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

Syntax

/\s/

 


Example

In the following code we will do a global search for the single space characters:

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

The white spaces in the text below will get a match:

Is this all there is?

 


Try-It-Yourself Demos

/s
How to use the "/s" modifier to find all the occurrences of single space characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


**************************

JavaScript RegExp \S Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the \S modifier is set the regular expression finds characters that is not single space characters.

A single space character can be:

  • A whitespace character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

Syntax

/\S/

 


Example

In the following code we will do a global search for the characters that is not single space:

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

The marked text below shows where the RegExp gets a match (matches everything except the whitespaces):

Is this all there is?

 


Try-It-Yourself Demos

/S
How to use the "/S" modifier to find all the occurrences of non single space characters using a string object.


RegExp Object Reference Complete RegExp Object Reference


***************************

JavaScript RegExp {x} Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the {x} modifier is set the regular expression finds a specific number of the regular expression grouped together.

The characters inside the brackets must be a number.

Syntax

/regexp{x}/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a search for any substring with four digits:

var str = "100 or 1000?";
var patt1 = /\d{4}/;

The marked text below shows where the RegExp gets a match:

100 or 1000?

 


Try-It-Yourself Demos

{x}
How to use the "{x}" modifier to find a specific number of specified characters grouped together using a string object.


RegExp Object Reference Complete RegExp Object Reference


*************************

JavaScript RegExp {x,} Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the {x,} modifier is set the regular expression finds a specific number or more of the regular expression grouped together.

The character inside the brackets must be a number.

Syntax

/regexp{x,}/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a global search for any substring with three or more digits:

var str = "100 or 1000?";
var patt1 = /\d{3,}/g;

The marked text below shows where the RegExp gets a match:

100 or 1000?

 


Try-It-Yourself Demos

{x,}
How to use the "{x,}" modifier to find a specific number or more of specified characters grouped together using the string object.


RegExp Object Reference Complete RegExp Object Reference


********************************

JavaScript RegExp ? Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the ? modifier is set the regular expression finds zero or one of a specific character.

Syntax

new RegExp("regexp?")
OR
/regexp?/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a global search for any substring with a 1 followed by zero or one "0" characters:

var str = "1, 100 or 1000?";
var patt1 = /10?/g;

The marked text below shows where the RegExp gets a match:

1, 100 or 1000?

 


Try-It-Yourself Demos

?
How to use the "?" modifier to find zero or one of a character using a string object.


RegExp Object Reference Complete RegExp Object Reference


**************************************

JavaScript RegExp * Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the * modifier is set the regular expression finds zero or more of a specific character.

Syntax

new RegExp("regexp*")
OR
/regexp*/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a global search for any substring with a 1 followed by zero or more "0" characters:

var str = "1, 100 or 1000?";
var patt1 = /10*/g;

The marked text below shows where the RegExp gets a match:

1, 100 or 1000?

 


Try-It-Yourself Demos

*
How to use the "*" modifier to find zero or more of a character using a string object.


RegExp Object Reference Complete RegExp Object Reference


**********************

JavaScript RegExp + Modifier


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

When the + modifier is set the regular expression finds one or more of a specific character.

Syntax

new RegExp("regexp+")
OR
/regexp+/

 

ParameterDescription
regexp Required. The rest of the regular expression to use

 


Example

In the following code we will do a global search for any substring with a 1 followed by one or more "0" characters:

var str = "1, 100 or 1000?";
var patt1 = /10+/g;

The marked text below shows where the RegExp gets a match:

1, 100 or 1000?

 


Try-It-Yourself Demos

+
How to use the "+" modifier to find one or more of a character using a string object.


RegExp Object Reference Complete RegExp Object Reference


*****************

JavaScript global Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The global property is used to return whether or not the "g" modifier is used in the regular expression.

The "g" modifier is set if the regular expression should be tested against all possible matches in the string.

This property is "TRUE" if the "g" modifier is set, "FALSE" otherwise.

Syntax

RegExpObject.global

 


Example

In the following example we will see if the global property is set:

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("W3");
if(patt1.global)
  {
  alert("Global property is set");
  }
else
  {
  alert("Global property is NOT set.");
  }
</script>

 


*******************************

JavaScript ignoreCase Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The ignoreCase property is used to return whether or not the "i" modifier is used in the regular expression.

The "i" modifier is set if the regular expression should ignore the case of characters.

This property is "TRUE" if the "i" modifier is set, "FALSE" otherwise.

Syntax

RegExpObject.ignoreCase

 


Example

In the following example we will see if the ignoreCase property is set:

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("W3");
if(patt1.ignoreCase)
  {
  alert("ignoreCase property is set");
  }
else
  {
  alert("ignoreCase property is NOT set.");
  }
</script>

 


******************************

JavaScript input Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The input property is the string the pattern match is performed.

Syntax

RegExp.input

 


Example

In the following example we will get the input string the regular expression vas performed on:

<script type="text/javascript">
function getRegInput()
{
var patt1 = new RegExp("W3");
var str = document.frm1.txtinput.value;
patt1.test(str);

if(RegExp.input)
  {
  alert("The input is: " + RegExp.input);
  }
else
  {
  alert("The input does not contain W3");
  }
}
</script>

<form name="frm1">
Enter some Text and click outside:
<input type="text" name="txtinput" onChange='getRegInput()'><br />
Input will only show if text contains W3.
</form>

 


*****************************

JavaScript lastIndex Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The lastIndex property specifies the index (placement) in the string where to start the next match.

The index is a number specifying the placement of the first character after the last match.

This property only works if the "g" modifier is set.

Syntax

RegExpObject.lastIndex

 


Example

In the following example we will see the index of the string after a match is found:

<script type="text/javascript">
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain", "g");
patt1.test(str);
document.write("Match found. index now at: " + patt1.lastIndex);
</script>

 


****************************************

JavaScript lastMatch Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The lastMatch property is the last matched characters.

Syntax

RegExp.lastMatch

 


Example

In the following example we will see the last match from the string:

<script type="text/javascript">
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain");
patt1.test(str);
document.write("Match found: " + RegExp.lastMatch);
</script>

 


********************************

JavaScript lastParen Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The lastParen property is the last parenthesized substring match.

Syntax

RegExp.lastParen

 


Example

In the following example we will see the last parenthesized substring of a string:

<script type="text/javascript">
var str = "Visit W3Schools (now)";
var patt1 = new RegExp("(now)", "g");

patt1.test(str);
document.write("Last parenthesized substring is: " + RegExp.lastParen);
</script>

 


********************************************

JavaScript leftContext Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The leftContext property is the substring in front of the characters most recently matched.

This property contains everything from the string from before the match.

Syntax

RegExp.leftContext

 


Example

In the following example we will see the text from the string in front of the last match:

<script type="text/javascript">
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain");
patt1.test(str);
document.write("Text before match: " + RegExp.leftContext);
</script>

 


****************************

JavaScript global Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The global property is used to return whether or not the "m" modifier is used in the regular expression.

The "m" modifier is set if the regular expression should be tested against possible matches over multiple lines.

This property is "TRUE" if the "m" modifier is set, "FALSE" otherwise.

Syntax

RegExpObject.multiline

 


Example

In the following example we will see if the multiline property is set:

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("W3","m");
if(patt1.multiline)
  {
  alert("Multiline property is set");
  }
else
  {
  alert("Multiline property is NOT set.");
  }
</script>

 


***********************************

JavaScript rightContext Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The rightContext property is the substring from after the characters most recently matched.

This property contains everything from the string from after the match.

Syntax

RegExp.rightContext

 


Example

In the following example we will see the text from the string from after the last match:

<script type="text/javascript">
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain");
patt1.test(str)
document.write("Text after match: " + RegExp.rightContext);
</script>

 


****************************************

JavaScript source Property


RegExp Object Reference Complete RegExp Object Reference


Definition and Usage

The source property is used to return the text used for pattern matching.

The returned text is everything except the forward slashes and any flags.

Syntax

RegExpObject.source

 


Example

In the following example we will get the text used for pattern matching:

<script type="text/javascript">
var str = "Visit W3Schools";
var patt1 = new RegExp("W3S","g");
document.write("The regular expression is: " + patt1.source);
</script>

 


*************************************

Terug naar de top