Keep it simple. Only allow underscore and alphanumeric regex:

/^[a-zA-Z0-9_]+$/

Javascript es6 implementation (works for React):

const re = /^[a-zA-Z0-9_]+$/;
re.test(variable_to_test);
Answer from GavinBelson on Stack Overflow
🌐
Regex Tester
regextester.com › 93960
Name - no symbols or special chars. - Regex Tester/Debugger
Regular Expression to Underscore, hyphen allowed. No special chars (@! etc.). Can't end with space. Can't yet figure out how to only allow space and not \t\r\n.
Discussions

Restrict users from inserting special character except underscore
Possible duplicate of Need Regex Pattern: Can't start w num; No special characters except underscore and hyphen; allows characters/nums More on stackoverflow.com
🌐 stackoverflow.com
Matching any character except an underscore using Regex - Stack Overflow
I'm trying to write Regex that will match a string of characters EXCEPT strings with an underscore. I have this so far /[A-Za-z0-9]+/ but I don't know what to include in it to make it require no More on stackoverflow.com
🌐 stackoverflow.com
php - Regular Expressions: How to Express \w Without Underscore - Stack Overflow
Is there a concise way to express: \w but without _ That is, "all characters included in \w, except _" I'm asking this because I'm looking for the most concise way to express domain name valida... More on stackoverflow.com
🌐 stackoverflow.com
Regex doesn't recognize underscore as special character - Stack Overflow
I'm trying to make a regex for password validation such that the password must be at least 8 chars and include one uppercase, one lowercase, one number, and one special char. It works fine except it won't recognize the underscore (_) as a special character. I.e., Pa$$w0rd matches, but Pass_w0rd doesn't. Thoughts? ... The answer is really very simple. You did not ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Alteryx Community
community.alteryx.com › home › participate › discussions › alteryx one
REGEX Count Special Characters Excluding underscore _ and period . - Alteryx
January 12, 2021 - i want to determine if a string has special characters other than string, (A-Z,a-z) and numbers (0-9) and as well as _ and . I just want to count if string has any special characters excluding or except underscore and period.
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
The metacharacter dot (.) matches any single character except newline \n (same as [^\n]). For example, ... matches any 3 characters (including alphabets, numbers, whitespaces, but except newline); the.. matches "there", "these", "the ", and so on. \w (word character) matches any single letter, ...
Find elsewhere
🌐
Reddit
reddit.com › r/regex › allow special characters but not spaces?
r/regex on Reddit: Allow special characters but not spaces?
December 23, 2020 -

Wondering if there is a clean way to do this where you could use \W instead of spelling out every one of the allowed special characters.

[A-Za-z0-9@!#$%&*()_+-=//.,";:{}|...etc etc] for example, is so ugly but it works to exclude spaces from a password validation while allowing other special characters, letters, and numbers.

I have not been successful in finding a way to neaten it up to [A-Za-z0-9\W] with something short and sweet to say "except for spaces" at the beginning, middle, end.

Any thoughts? Thanks!

🌐
Coderanch
coderanch.com › t › 406470 › java › Regex-replacing-special-characters
Regex for replacing special characters (Beginning Java forum at Coderanch)
March 19, 2007 - Originally posted by srikanth ramu: This will replace all the special characters except dot (.) String newName = name.replaceAll("[\\W]&&[^.]","_"); Thanks for replying. But I want all the characters apart from A-Z a-z 0-9 . (dot) _ (underscore) - (hyphen) to be replaced with an _ (underscore) ... To invert a character class, add '^' to the beginning of it: Also, you want to use the + (one or more) quantifier instead of * (zero or more). Otherwise, you end up inserting an underscore at every other position. ... I'm not sure you need a + either.
🌐
Regex Tester
regextester.com › 96992
characters and numbers with underscore - Regex Tester/Debugger
Regex Tester is a tool to learn, build, & test Regular Expressions (RegEx / RegExp). Results update in real-time as you type. Roll over a match or expression for details. Save & share expressions with others. Explore the Library for help & examples. Undo & Redo with {{getCtrlKey()}}-Z / Y. Search for & rate Community patterns. ... extended (x) extra (X) single line (s) unicode (u) Ungreedy (U) Anchored (A) dup subpattern names(J) ... Url checker with or without http:// or https:// Match string not containing string Check if a string only contains numbers Only letters and numbers Match elements
🌐
RegExr
regexr.com › 3a8p3
RegExr: alphanumeric, underscore and .
Welcome to RegExr v2.0 by gskinner.com! ... Edit the Expression & Text to see matches. Roll over matches or the expression for details. Undo mistakes with ctrl-z. Save & Share expressions with friends or the Community. A full Reference & Help is available in the Library, or watch the video Tutorial. ... Build a suite of tests that your expression should (or should not) match.
🌐
UiPath Community
forum.uipath.com › help › studio
Regex to check if string has special characters except dash and comma and split - Studio - UiPath Community Forum
September 30, 2020 - If string contains special character like hello-world but not dash(/) and comma (,) then split get the string output which is input: hello-world output : world input : hi,hello output: hi,hello input : my-friend output: friend Thanks