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