RegExp
constructor
JS Home <<
JS Reference <<
RegExp
Regular expressions creation.
Description
In JavaScript regular expressions are created using the constructor RegExP
or created using literal format.
Syntax
Signature | Description |
---|---|
var aRegExp = new RegExp(pattern [, flags]); | Creates a wrapper object using the constructor RegExP . |
var bRegExp = /pattern/flags; | Creates a wrapper object using literal format. |
Parameters
Parameter | Description |
---|---|
pattern | The regular expression pattern. |
flags | Flags can be used individually or in combination and are applied to the characters we are searching with the regular expression pattern.
|
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type RegExp . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the RegExp function that created the prototype. |
global | Returns true if global match flag set or false otherwise. |
ignoreCase | Returns true if ignore case flag set or false otherwise. |
lastIndex | Index to start next match from. |
multiline | Returns true if match each line flag set or false otherwise. |
source | Returns the text of the pattern. |
Class Methods
None.
Instance Methods
Instance Method | Description |
---|---|
Getter (Accessor) MethodsThese methods DO NOT modify the Regexp object |
|
exec() | Returns a result array, or null from an executed search in a specified string. |
test() | Returns true or false for a match between a regular expression and a string. |
toString() | Returns a string representation of the RegExp object. |
Setter (Mutator) MethodsThese methods DO modify the Regexp object |
|
None. |
Special Character Usage In Regular Expressions
Character | Meaning | Examples |
---|---|---|
Escape/Unescape | ||
\ | Used to escape characters that are treated literally within regular expressions or alternatively to unescape special characters | Literal Content /d/ matches the character d /\d/ matches a digit character Unescape Special Characters /d+/ matches one or more character d /d\+/ matches d+ |
Quantifiers | ||
* | Matches preceding item 0 or more times | /do*/ Every dig has its day Every dog has its day Shut that doooor Can you see me |
? | Matches preceding item 0 or 1 times | /do?/ Every dig has its day Every dog has its day Shut that doooor Can you see me |
+ | Matches preceding item 1 or more times | /do+/ Every dig has its day Every dog has its day Shut that doooor Can you see me |
^ | Matches beginning of input If line match flag (m) is set will also match after a line break character. |
/^A/ an Armadillo An Armadillo |
$ | Matches end of input If line match flag (m) is set will also match before a line break character. |
/Z$/ BuzZ BuZz BuzZ BuZZ |
x (?=y ) |
Matches Regexp(x ) only if followed by y |
/and(?= five)/ one and two and three one and two and four one and two and five |
x (?!y ) |
Matches Regexp(x ) only if NOT followed by y |
/and(?! five)/ one two and three one two and four one two and five |
{n } |
Matches exactly n occurrences of the preceding item.Where n is a positive integer |
/o{4}/ one two one twoo one twooo one twoooo |
{n, } |
Matches at least n occurrences of the preceding item.Where n is a positive integer |
/o{3,}/ one two one twoo one twooo one twoooo |
{n,m } |
Matches at least n and at most m occurrences of the preceding item.Where n and m are positive integers |
/o{2,3}/ one two one twoo one twooo one twoooo |
Special Characters | ||
. | Matches any single character except newline characters. | /.t/ This Time tonight this is good |
x |y |
Matches x or y |
/three|four/ one two one two three one two four |
[xyz] | A character set. Matches any of the enclosed characters. You can specify a range of characters by using a hyphen. |
/[eno]/g one two one twoo one twooo one twoooo |
[^xyz] | A negated character set. Matches anything not enclosed in the brackets. You can specify a range of characters by using a hyphen. |
/[^eno]/g one two one twoo one twooo one twoooo |
\b | Find a match at the beginning or end of a word. | At Beginning /\bday/g the day today is saturday daytime and nighttime At End /day\b/g the day today is saturday day and nighttime |
\B | Find a match NOT at the beginning or end of a word. | Not At Beginning /\Bday/g the day today is saturday daytime and nighttime Not At End /day\B/g the day today is saturday day and nighttime |
\d | Find a digit character. Same as the range check [0-9]. |
/\d/g/ Was it 76 or 77 |
\D | Find a non-digit character. Same as the range check [^0-9]. |
/\D/g/ Was it 76 or 77 |
\0 | Find a Nul character. Do not follow this regualr expression with another digit |
/\0/ |
\s | Find a whitespace character. | Example below words are greyed out and spaces are highlighted in red purely for emphasis /\s/g/ Beware of the dog |
\S | Find a non-whitespace character. | Example below spaces are grayed out for emphasis /\S/g/ Beware of the dog |
\w | Find a word character. A word character is a character in ranges a-z, A-Z, 0-9 and also includes the _ (underscore) symbol. Same as the range check [A-Za-z0-9_]. |
/\w/g/ 76% off_sales. £12 only |
\W | Find a non-word character. Same as the range check [^A-Za-z0-9_]. |
/\W/g/ 76% off_sales. £12 only |
\xnn |
Find a character that equates to hexadecimal nn .Where nn is a two digit hexadecimal number |
/\x70/ The quick brown fox jumps. |
\unnnn |
Find a character with the unicode value nnnn .Where nnnn is a four digit hexadecimal number |
/\u0065/ The quick brown fox jumps. |
Control Characters | ||
[\b] | Find a backspace character. | /[\b]/ Will search for backspace. The brackets differentiate this from \b special character |
\cA |
Find a control character A .Where A is the control character in range A-Z you are looking for. |
/\cC/ Will search for control-C in a string. |
\f | Find a formfeed character. | /\f/ When matched will return the formfeed character When searched will return the zero-based index position the formfeed character was found in. |
\n | Find a newline character. | /\n/ When matched will return the newline character When searched will return the zero-based index position the newline character was found in. |
\r | Find a carriage return character. | /\r/ When matched will return the carriage return character When searched will return the zero-based index position the carriage return character was found in. |
\t | Find a tab character. | /\t/ When matched will return the tab character When searched will return the zero-based index position the tab character was found in. |
\v | Find a vertical tab character. | /\v/ When matched will return the vertical tab character When searched will return the zero-based index position the vertical tab character was found in. |
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions