Need Help With Regex

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

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
console.log(str.match(regex))

^ 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.