How to Use Regex in TypeScript
-
Use
RegExp
in TypeScript -
Use the
test
Method to Check the Presence of a Pattern in the Target String Using TypeScript -
Use String
match
orexec
Method ofRegExp
to Find Matches in Target String Using TypeScript -
Use Flags in
RegExp
Literal Expressions in TypeScript
RegExp
are regular expressions used to match a fixed set of characters in a target string using TypeScript.
There are many attributes and methods associated with the RegExp
object. Each of the attributes to different options while pattern matching.
Use RegExp
in TypeScript
There are two ways by which Regex can implement in TypeScript. One of them is assigning a regular expression literal type RegExp
.
const rExp : RegExp = /[A-C]/g;
Another way of expressing regular expressions is through the RegExp
constructor.
const rExp : RegExp = new RegExp("[A-C]", "g");
For the rest of the tutorial, constant regular expressions will be used for matching the test strings.
Use the test
Method to Check the Presence of a Pattern in the Target String Using TypeScript
The test method of RegExp
can be used to check if a certain pattern or regular expression is present in the target string by returning a boolean
variable.
const targetString : string = "All is well";
// regex to check if 'All' word is present or not.
const rExp : RegExp = /All/;
console.log(rExp.test(targetString));
Output:
true
Use String match
or exec
Method of RegExp
to Find Matches in Target String Using TypeScript
The match
method in string or the exec
method of RegExp
can find occurrences in the target string corresponding to the regular expression.
Both methods behave similarly for finding the first match in the target string.
const targetString : string = "All is well";
const rExp : RegExp = /All/;
console.log(rExp.exec(targetString));
console.log(targetString.match(rExp))
Output:
["All"]
["All"]
The functions differ in finding all cases of the matched string or searching with the global flag set.
const targetString : string = "All is well and Beautiful";
const rExp : RegExp = /[A-C]/g;
console.log(rExp.exec(targetString));
console.log(targetString.match(rExp))
Output:
["A"]
["A", "B"]
However, the exec
function can also find the total number of matches in the target string corresponding to the regular expression.
function getMatches( target : string, rExp : RegExp, matches : Array<RegExpExecArray> = []) {
const matchIfAny = rExp.exec(target);
matchIfAny && matches.push(matchIfAny) && getMatches(target, rExp, matches);
return matches;
}
const rExp : RegExp = /[A-C]/g
const test_string : string = "All is Well and Beautiful";
console.log(getMatches(test_string, rExp, []).map( arr => arr[0]));
Output:
["A", "B"]
Use Flags in RegExp
Literal Expressions in TypeScript
Some common flags in regular expression include the ignore cases flag i
, global flag g
, and the multiline flag m
.
The following code segment demonstrates how the i
and the m
flags can be used.
const targetString : string = `#1 First line
#2 Second Line
#3 Third liNe`;
// regex to check word is present or not.
const rExp1 : RegExp = /line/g;
const rExp2 : RegExp = /line/gi;
const rExp3 : RegExp = /^#\d/gi;
const rExp4 : RegExp = /^#\d/gim;
console.log(targetString.match(rExp1));
console.log(targetString.match(rExp2));
console.log(targetString.match(rExp3));
console.log(targetString.match(rExp4));
Output:
["line"]
["line", "Line", "liNe"]
["#1"]
["#1", "#2", "#3"]
Thus the i
flag captured all occurrences of line
irrespective of the case.
The m
flag is usually useful for finding patterns at the start and end of lines.
In this example, with the m
flag set, all the occurrences starting with a number at the start of a line could be found.