I have a string that has a specific pattern at the start, that I need to match until it stop occuring, meaning match pattern once until it's broken. The pattern is a digit(s) and a specific (d,h,m,s) character.
For example, 3days 50 seconds 10 min lorem ipsum consectetur adipiscing elit 24 sec sit ametelit
has to match 3days
, 50 seconds
and 10 min
, but not 24 sec
because it occurs after the pattern was broken.
I have already written a RegEx that matches a digit and character, /\d{1,8}\s*(d|h|m|s)/g
. This will match a number of digits at the start, ignore whitespace and then check if a specific character occurs, so it returns these matches 3d
, 50 s
and 10 m
.
However, it matches everything that passes the test, even 24 s
, which I don't want it to do. If I remove /g
, then it will expectedly return only the first result, which is also undesireable.
CodePudding user response:
const units = ['day', 'days', 'hour', 'hours', 'minute', 'minutes', 'min', 'mins', 'second', 'seconds', 'sec', 'sec']
const regexp = new RegExp(String.raw `(?:\d{1,2}\s*(?:${units.join('|')})\s ) `, 'g')
const text = '3days 50 seconds 10 min lorem ipsum consectetur adipiscing elit 24 sec sit ametelit'
for (const match of text.matchAll(regexp)) {
console.log(match)
}
You can also match any word
const regexp = /(?:\d{1,2}\s*[a-z] \s ) /g
const text = '3days 50 seconds 10 min lorem ipsum consectetur adipiscing elit 24 sec sit ametelit'
for (const match of text.matchAll(regexp)) {
console.log(match)
}