Regular Expressions, references
-
YouTube: Best of Fluent 2012: /Reg(exp){2}lained/: Demystifying Regular Expressions
-
RegExp()
most popular instance methods:-
rx.exec(str)
- executes a search for a match in a specified string. Returns a resultarray
, ornull
. Example with named capture groups. -
rx.test(str)
- executes a search for a match between a regular expression and a specified string. Returnstrue
orfalse
.
-
-
String()
most popular instance methods:-
str.match(rx)
- retrieves the result of matching a string against a regular expression.-
const res = str.match(/rx/)
- returns an array like object whereres[0]
contains the matched string. -
const res = str.match(/rx/g)
- returns an array/iterator of the matches - interchangeable of some simple usages ofstr.matchAll(rx)
andrx.exec(str)
.
-
-
str.matchAll(rx)
- returns an iterator of all results matching a string against a regular expression, including capturing groups - interchangeable ofstr.matchAll(rx)
. -
str.replace(rx, 'replacement')
- returns a new string with some or all matches of apattern
replaced by a replacement. Thepattern
can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced. WithRexExp
and the global/g
flag (switch) it can be used of interchangeable ofstr.replaceAll(rx)
-
str.replaceAll(rx, 'replacement')
- returns a new string with all matches of apattern
replaced by areplacement
. Thepattern
can be a string or a RegExp, and thereplacement
can be a string or a function to be called for each match. -
str.search(rx)
executes a search for a match between a regular expression and this String object. Returns the index of the first match between the regular expression and the given string, or-1
if no match was found.If you only want to know if it exists, use the similar
test()
method on theRegExp
prototype, which returns a boolean and it is faster. -
str.split(separator, limit)
- divides aString
into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where thepattern
is provided as the first parameter in the method’s call.The
separator
can be a simple string or it can be a RegExp. Ifseparator
is a regular expression that contains capturing parentheses ( ), matched results are included in the array.
-
Exercises and the most interesting parts
Flag | Description | Corresponding property |
---|---|---|
d |
Generate indices for substring matches. | RegExp.prototype.hasIndices |
g |
Global search. | RegExp.prototype.global |
i |
Case-insensitive search. | ` RegExp.prototype.ignoreCase` |
m |
Multi-line search. | RegExp.prototype.multiline |
s |
Allows . to match newline characters. |
RegExp.prototype.dotAll |
u |
“unicode”; treat a pattern as a sequence of unicode code points. | RegExp.prototype.unicode |
y |
Perform a “sticky” search that matches starting at the current position in the target string. See sticky . |
RegExp.prototype.sticky |
-
Note that the flags are an integral part of a regular expression. They cannot be added or removed later.
-
Creating regular expressions by the
RegExp()
constructor is preferable in the cases where we need to create a regexp dynamically from a string, for example based on the user’s input or some other variable.let rxVar = '\b\d+?\b'; let rxVarFlags = 'ig'; const rh = new RegExp(rxVar, rxVarFlags);
In all other cases we should prefer the literal syntax - it’s shorter; it’s faster - because the regex object is created once…
const rx = /\b\d+?\b/ig;
-
Phone number exercise:
exercise-1.validate-phone-number.html