Regex matching JS
Regular expressions are their own prototype in JS - RegExp.
If you just want to match substrings inside another string:
js
// This has the global flag 'g' set to do multiple matches for a single string
const CAPITAL_LETTERS_PATTERN = /[A-Z]/g;
const fruits = "AppleBananaPomegranateOrange";
// ["A", "B", "P", "O"]
const matches = fruits.match(CAPITAL_LETTERS_PATTERN);When to use which function
- To know if a string matches (boolean), use
.test() - To get a single match, use
.exec() - To get multiple matches, use
.match() - To get multiple matches using capture groups (the global flag must be set), use
.exec()or.matchAll()
INFO
See String.prototype.match() - JavaScript | MDN See RegExp.prototype.test() - JavaScript | MDN
Regular expression flags
Regular expressions are of the form:
js
// Shorthand
const re = /pattern/flags;
// With the RegExp constructor
const re = new RegExp(pattern, flags);Where flags is a sequence of none, one or more these flags:
dGenerate indices for substring matches.gGlobal search.iCase-insensitive search.mMakes ^ and $ match the start and end of each line instead of those of the entire string.sAllows . to match newline characters.u"Unicode"; treat a pattern as a sequence of Unicode code points.vAn upgrade to the u mode with more Unicode features.yPerform a "sticky" search that matches starting at the current position in the target string.
(Yoinked directly from the MDN documentation)
Each flag also corresponds with a boolean property on the finished RegExp object. For example, g corresponds with RegExp.prototype.global:
js
const re = /hello-there/g;
console.log(re.global); // -> true