Need Help With Regex

Hi experts, I need help to form a regex formula to only include the 5W1H from a set of data.
Why What Where Who When How

It should only extract out these words only, and it should not include words with terms like “wholesale” “Howard Stone” which has nothing to do with 5W1H questions.

So the pipe method doesn’t work. Can anyone help? A clean code will be best. I just need the word extracted, nothing esle is required. Only required is that the word can’t be used inside another word. So I’m looking for a formula like

What (no letters before or after this!), etc.

For example:

How do I make a puppy chain?

Where can I buy dog food?

When should I bath my dog?

And not:

Dog food prices

HOWard’s dog food brand

Do you want to get only the words at the beginning of the string?
A simple regex for that can be made like so

let str = "Where can I buy dog food?"
let regex = /^who|what|when|where|why|how(?=\s)/i

^ Start from the beginning
who|what|... check for any of these words
(?=\s) a lookahead, checks if there is a space at the end of the word
i ignores capitalization

Show as an example of what you’ve tried and we can go from there.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.