How to exclude _ from \w in Regular Expression?

I searched for a while and found this

let regEx2=/\w(?!_)/

I was wondering whether it is possible to make it work with something short like this:

let regEx= /[\w^_]/

Or is there any other way you can exclude _ from \w except this long way:

let regEx3=/[a-z][A-Z][0-9]/

@ankurchaulagain I think this should be in #javascript category.

I edited and changed the category. Thank you.

Use this to test out what you come up with: regex101: build, test, and debug regex. Remember to select the correct flavour on the left hand side.

Personally I think the best option is just /[a-z0-9]/i or if you don’t want the i flag /[a-zA-Z0-9]/, or you can substitute0-9 with \d.

If you insist on using \w, you can use lookahead assertion i.e. try /(?=[^_])\w/, or, worse, lookbehind assertion /\w(?<=[^_])/ both of which I think are actually less elegant.

1 Like

Thank you. My original though was to use that range way but was just curious of other ways.

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