Cris Ryan Tan Cris Ryan Tan - 10 months ago 34
Javascript Question

How to capture multiple succeeding words on regex

Hi I created a regex that would detect a question where it is starting with
"What is _____?"

(?:what is ([a-zA-Z]+)\?)

It works only if the word after "What is" is only one word.

"What is dinner" // returns true

for example, but if I add another word in my question

what is breakfast and lunch? // returns false

It returns
, what do I need to add for the succeeding words in my question to return true in my regex pattern?


Use this regex: (?:[Ww]hat is (([a-zA-Z]+\s*)+)\s*\?)

In this \1 will return the part after "What is".

Try here.

(([a-zA-Z]+\s*)+) will match multiple space separated words.

Regular expression visualization