ES6 - RegExp

A regular expression is an object that describes a pattern of characters. Regular expressions are often abbreviated “regex” or “regexp”.
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 the text.
A regular expression can be defined as −
var pattern = new RegExp(pattern, attributes); 
OR 
var pattern = /pattern/attributes;

The attribute can have any combination of the following values.
G
Global Match
I
Ignore case
M
Multiline; treat the beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or the end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)
U
Unicode; treat the pattern as a sequence of unicode code points
Y
Sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes)
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.
[...]
Any one character between the brackets
[^...]
Any one character not between the brackets
[0-9]
It matches any decimal digit from 0 through 9
[a-z]
It matches any character from lowercase a through lowercase z
[A-Z]
It matches any character from uppercase A through uppercase Z
[a-Z]
It matches any character from lowercase a through uppercase Z
The ranges shown above are general; you could also use the range [0-3] to match any decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character ranging from b through v.
The frequency or position of the bracketed character sequences and the single characters can be denoted by a special character. Each special character has a specific connotation. The +, *, ?, and $ flags all follow a character sequence.
p+
It matches any string containing at least one p.
p*
It matches any string containing zero or more p's
p?
It matches any string containing one or more p's
p{N}
It matches any string containing a sequence of N p's
p{2,3}
It matches any string containing a sequence of two or three p's
p{2, }
It matches any string containing a sequence of at least two p's
p$
It matches any string with p at the end of it
^p
It matches any string with p at the beginning of it
[^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
Alphanumeric
Itself
\0
The NULL character (\u0000)
\t
Tab (\u0009)
\n
Newline (\u000A)
\v
Vertical tab (\u000B)
\f
Form feed (\u000C)
\r
Carriage return (\u000D)
\xnn
The Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n
\uxxxx
The Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t 
\cX
The control character ^X; for example, \cJ is equivalent to the newline character  \n 
A meta-character is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.
For instance, you can search for a large sum of money using the '\d' meta-character: /([\d]+)000/. Here, \d will search for any string of the numerical character.
The following table lists a set of meta-characters which can be used in PERL Style Regular Expressions.
.
A single character
\s
A whitespace character (space, tab, newline)
\S
Non-whitespace character
\d
A digit (0-9)
\D
A non-digit
\w
A word character (a-z, A-Z, 0-9, _)
\W
A non-word character
[\b]
A literal backspace (special case)
[aeiou]
Matches a single character in the given set
[^aeiou]
Matches a single character outside the given set
(foo|bar|baz)
Matches any of the alternatives specified
A string that contains the flags of the RegExp object
Whether to test the regular expression against all possible matches in a string, or only against the first
Whether to ignore case while attempting a match in a string
Whether read/write property of RegExp objects.
Whether or not to search in strings across multiple lines
The text of the pattern
Executes a search for a match in its string parameter
Tests for a match in its string parameter
Performs a match to the given string and returns the match result
Replaces matches in the given string with a new substring
Searches the match in the given string and returns the index the pattern found in the string
Splits the given string into an array by separating the string into substring
Returns a string representing the specified object. Overrides theObject.prototype.toString() method

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    40 Lectures 
     5 hours 

 Richa Maheshwari

    16 Lectures 
     1 hours 

 Anadi Sharma

    50 Lectures 
     6.5 hours 

 Gowthami Swarna

    14 Lectures 
     1 hours 

 Deepti Trivedi

    31 Lectures 
     1.5 hours 

 Shweta
 Print
 Add Notes
 Bookmark this page