JavaScript – How to Match Multiple Parts of a String Using RegExp?
In JavaScript, the regular expression (RegExp) object provides a powerful mechanism to search, match, and manipulate strings. One of the common use cases is matching multiple parts of a string that satisfy a certain pattern. You can easily do this using global (g) and other RegExp features.
1. Using Global (g) Flag to Match Multiple Occurrences
The g flag enables global matching, which allows the regular expression to match all occurrences of a pattern in the string, not just the first one.
let s = "apple orange apple banana apple";
let regex = /apple/g;
let res = s.match(regex);
console.log(res);
Output
[ 'apple', 'apple', 'apple' ]
- /apple/g matches the word “apple” globally.
- The match() method returns an array of all matched parts of the string.
2. Using match() with Capture Groups
You can use parentheses () to define capture groups in a regular expression. This allows you to match and extract multiple parts of the string that satisfy different conditions.
let s = "apple123 orange456 apple789";
let regex = /(\w+)(\d+)/g;
let res = [...s.matchAll(regex)];
console.log(res);
Output
[ [ 'apple123', 'apple12', '3', index: 0, input: 'apple123 orange456 apple789', groups: undefined ], [ 'orange456', 'orange45', '6', index: 9, input: ...
- (\w+) matches the word characters (letters) and (\d+) matches digits.
- matchAll() returns all matches as an array of arrays, where each sub-array contains the full match and the captured groups.
3. Using exec() with Global Flag
The exec() method allows you to find multiple matches one at a time. It returns an array for each match, and with the global flag, it can be used in a loop to find all matches.
let s = "apple123 orange456 apple789";
let regex = /(\w+)(\d+)/g;
let ress;
while ((res = regex.exec(s)) !== null) {
console.log(res);
}
Output
[ 'apple123', 'apple12', '3', index: 0, input: 'apple123 orange456 apple789', groups: undefined ] [ 'orange456', 'orange45', '6', index: 9, input: 'apple123 orange456 apple789', ...
- regex.exec(s) finds one match at a time, and with each iteration, it returns the full match and the captured groups.
- The while loop continues until all matches are found.
4. Using split() to Split String by Multiple Parts
The split() method can be used with a regular expression to break a string into multiple parts. If the regular expression matches multiple segments, the split() method will break the string accordingly.
let s = "apple123 orange456 apple789";
let regex = /\d+/g;
let res = s.split(regex);
console.log(res);
Output
[ 'apple', ' orange', ' apple', '' ]
- /\d+/g matches sequences of digits.
- split() breaks the string wherever a match is found, returning an array of non-matching parts.
5. Using Non-Capturing Groups to Match Multiple Patterns
Non-capturing groups (?:…) allow you to combine multiple patterns without creating separate capturing groups. This is useful when you need to match multiple patterns in a string but don’t need to extract the individual parts.
let s = "apple123 orange456 apple789";
let regex = /(?:apple|orange)\d+/g;
let res = s.match(regex);
console.log(res);
Output
[ 'apple123', 'orange456', 'apple789' ]
- (?:apple|orange) matches either “apple” or “orange”.
- \d+ matches one or more digits after “apple” or “orange”.
- The g flag ensures all matching parts are found in the string.
6. Matching Multiple Patterns with OR (|) Operator
The | (OR) operator in regular expressions allows you to match one pattern or another. This can be used to match multiple parts of a string that satisfy different conditions.
let s = "apple orange123 banana456";
let regex = /apple|orange\d+/g;
let res = s.match(regex);
console.log(res);
Output
[ 'apple', 'orange123' ]
- /apple|orange\d+/g matches either “apple” or “orange” followed by one or more digits.
- The g flag ensures that all matches are found in the string.