Javascript regexp to define word boundary with parentheses

I have the line "I'm a robot, they called me 456 / m (4). Forget the name (it doesn't mean anything)"

Now I would like to extract all the words from this line for this I use a regular expression:

/\b[\w\S]+\b/g 

he returns me all the words in the string, except that there is the word “456 / (4” instead of “456 / (4).” I understand that this is due to the fact that this is the word boundary, but is there a way that could I say that this is not a legal word boundary, since there were no "legal" parentheses?

+5
source share
1 answer

I made it even better. He does exactly what you want.

 \b(?>\([\w\/]+\)|[\w\/])+ 

Regex101

If you need a javascript compatible version:

 ((?:(?=(\([\w\/]+\)|[\w\/]))\2)+) 

Just use capture group # 1 here.

Regex101

+4
source

All Articles