I am trying to extract placeholders for date and time parts from a string. While it seemed straightforward enough at first I am having trouble matching subsequent sequences.
These are two test strings:
home at hh:mm:ss for dinner with Bob
home at h:m:s for dinner with Bob
I want to match only the 'h', 'm', and 's' from the place holder. In this case they're grouped together but they can also appear spread out.
My first attempt was matching all the 'h', 'm', and 's' characters
const
tests = ['home at hh:mm:ss for dinner with Bob', 'home at h:m:s for dinner with Bob'],
regex = /(h+|m+|s+)/g;
tests.forEach((test, index) => {
console.group(`test ${index}`);
let match;
while (match = regex.exec(test)) {
console.log(match[1], match.index);
}
console.groupEnd();
});
This also matches the 'h' and 'm' in 'home' and the 'h' in 'with. To prevent this from happening I want to modify the regex to stop matching characters in a word.
const
tests = ['home at hh:mm:ss for dinner with Bob', 'home at h:m:s for dinner with Bob'],
regex = /(h+|m+|s+)(?:$|\W|\s)/g;
tests.forEach((test, index) => {
console.group(`test ${index}`);
let match;
while (match = regex.exec(test)) {
console.log(match[1], match.index);
}
console.groupEnd();
});
This change was a step in the right direction. The characters in 'home' are no longer matched but the 'h' in 'with' is still matched by the regex. This is the point were I am stuck. I've tried to modify regex to deal with this situation but without any success. This is my latest attempt:
const
tests = ['home at hh:mm:ss for dinner with Bob', 'home at h:m:s for dinner with Bob'],
regex = /(?:^|\W|\s)(h+|m+|s+)(?:$|\W|\s)/g;
tests.forEach((test, index) => {
console.group(`test ${index}`);
let match;
while (match = regex.exec(test)) {
console.log(match[1], match.index);
}
console.groupEnd();
});
It no longer matches the 'h' in 'with' but now it also no longer matches the 'mm' in 'hh:mm:ss'.
Can anyone help me with the regex I'm trying to construct?