A javascript regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text. Lets start learning javascript regular expression
Syntax
var pattern = new RegExp(pattern, modifier); OR /pattern/modifier;
Here is the description of the parameters −
- Pattern − A string that specifies the pattern of the regular expression or another regular expression.
- Modifiers − An optional string containing any of the “g”, “i”, and “m” attributes that specify global, case-insensitive, and multi-line matches, respectively.
Example
var pattern = /w3path/i;
Example explained:
/w3path/i
is a regular expression.
w3path
is a pattern
i
is a attributes (for case-insensitive).
Regular Expression Modifiers
Modifiers can be used to perform case-insensitive more global searches:
Modifier | Description |
i | Perform case-insensitive matching |
g | Perform a global match (find all matches rather than stopping after the first match) |
m | Perform multiline matching |
Brackets
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.
Expression | Description |
[…] | Any one character between the brackets. |
[abc] | Find any of the characters between the brackets. |
[^abc] | Find any character NOT between the brackets |
[0-9] | Find any of the digits between the brackets |
(x|y) | Find any of the alternatives separated with | |
Metacharacters are characters with a special meaning:
Metacharacter | Description |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
Quantifiers define quantities:
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{X} | Matches any string that contains a sequence of X n‘s |
n$ | Matches any string with n at the end of it |
^n | Matches any string with n at the beginning of it |
?=n | Matches any string that is followed by a specific string n |
?!n | Matches any string that is not followed by a specific string n |
Examples
Following examples explain more about matching characters.
Expression | Description |
[^a-zA-Z] | It matches any string not containing any of the characters ranging from a through z and A through Z. |
p.p | It matches any string containing p, followed by any character, in turn followed by another p. |
^.{2}$ | It matches any string containing exactly two characters. |
<b>(.*)</b> | It matches any string enclosed within <b> and </b>. |
p(hp)* | It matches any string containing a p followed by zero or more instances of the sequence hp. |
RegExp Properties
Property | Description |
constructor | Specifies the function that creates an object’s prototype. |
global | Specifies if the “g” modifier is set. |
ignoreCase | Specifies if the “i” modifier is set. |
lastIndex | The index at which to start the next match. |
multiline | Specifies if the “m” modifier is set. |
source | The text of the pattern. |
RegExp Methods
Here is a list of the methods associated with RegExp along with their description.
Method | Description |
---|---|
exec() | Executes a search for a match in its string parameter. |
test() | Tests for a match in its string parameter. |
toSource() | Returns an object literal representing the specified object; you can use this value to create a new object. |
toString() | Returns a string representing the specified object. |
Real life example of the JavaScript regular expression
<script> // only string var expr = new RegExp(/[a-zA-Z]/) expr.test('ab') // Output: true </script>
First time visiting your website, I really like your website!