Need Help to Understand RegEx in Checking for Palindromes Challenge

I solved the challenge using this /[^A-Za-z0-9]/g to remove any non-word character. It worked. After checking in a Medium article and in this forum to look for different kind of solutions to the challenge, I found this: /\W_/g .
I understand \W this is to remove any non-word character, but what the hell about the underscore _ ??? Why ??? Thanks for the help

Here are a couple tools to help understand regex.

For seeing how things match: https://regex101.com/

For cool graphics: http://emailregex.com/
(click thru to the “Regex Visual Tester”)

1 Like

The MDN reference on RegEXP was pretty good, you can find out exactly why \W works

\w is the same as [A-Za-z0-9_], and \W is the same as [^A-Za-z0-9_]

Regexr is good too if you’re a kinesthetic or visual learner.

1 Like