I have a regexp that matches all ascii characters:
/^[\x00-\x7F]*$/
Now I need to exclude from this range the following characters: ', ". How do I do that?
You can use negative lookahead for disallowed chars:
/^((?!['"])[\x00-\x7F])*$/
RegEx Demo
(?!['"]) is negative lookahead to disallow single/double quotes in your input.
You can exclude characters from a range by doing
/^(?![\.])[\x00-\x7F]*$/
prefixed it with (?![\.]) to exlude . from the regex match.
or in your scenario
/^(?!['"])[\x00-\x7F]*$/
Edit:
wrap the regex in braces to match it multiple times
/^((?!['"])[\x00-\x7F])*$/
The IMO by far simplest solution:
/^[\x00-\x21\x23-\x26\x28-\x7F]*$/
Related
I'm not got, yet, with regex. I've been trying to break my head to get this to work.
I need a regex that allows the user to enter
Any alphabetical char (a-z)
Any number
For special char only "-" and "_".
"#" is not allowed.
I got this but no dice. [^a-zA-Z0-9]
Thanks
^[\w-]+$
will match a string following the rules you describe. \w matches letters, digits, or underscore, then it adds - to that set. Anchoring with ^ and $ requires all the characters in the string to match this pattern.
remove ^ character in square brackets because is negative ranges, add some \-\_ to allow '-' and '_' character inside square brackets
[a-zA-Z0-9\-\_]+
I want a regex matching a specific word that is not surrounded by any alphanumeric character. My thought was to include a negation before and after:
[^a-zA-Z\d]myspecificword[^a-zA-Z\d]
So it would match:
myspecificword
_myspecificword_
-myspecificword
And not match:
notmyspecificword
myspecificword123
But this simple regex won't match the word by itself unless it is preceeded by a whitespace:
myspecificword // no match
myspecificword // match
Using the flags "gmi" and testing with JavaScript. What am I doing wrong? Shouldn't it be as simple as that?
https://regex101.com/r/BCkbVQ/3
Try using:
(?<![^\s_-])myspecificword(?![^\s_-])
This says to match myspecificword when it surrounded, on both sides, by either the start/end of the input, whitespace, underscore, or dash.
Demo
It is not whitespace that is required but any symbol that is matches [^a-zA-Z\d].
You should use: (Demo)
(?:^|[^a-zA-Z\d])myspecificword(?:[^a-zA-Z\d]|$)
The main benefit is support across all Regexp parsers.
If you truly mean "not surrounded by alphanumerics other than _ (and in your attempted regex you seem to be willing to match anything that isn't a letter or digit), then any of the following should be acceptable:
'myspecificword'
'_myspecificword_'
' myspecificword '
'-myspecificword-'
'(myspecificword)'
And the regex should be:
(?<![^_\W])myspecificword(?![^_\W])
let tests = ['myspecificword',
'_myspecificword_',
' myspecificword ',
'-myspecificword-',
'(myspecificword)',
'amyspecificword',
'1myspecificword'
];
let regex = /(?<![^_\W])myspecificword(?![^_\W])/;
for (let test of tests) {
console.log(regex.test(test));
}
The "accepted" answer will not match (myspecificword), for example.
The title of this question is
Regex for word not surrounded by alphanumeric characters
The other answers have all addressed a different question (which may well be the one intended):
Regex for word neither preceded nor followed by alphanumeric characters
I will refer to these statements as #1 and #2 respectively.
If the specified word were 'cat' and the string were '9cat', 'cat' is not surrounded by alphanumeric characters in the string, so there is a match with #1, but not with #2.
For #1, one could use the regex:
/cat(?!\p{Alpha}|(?<!\p{Alnum})cat/
("match 'cat' not followed by a Unicode alphanumeric character or 'cat' not preceded by a Unicode alphanumeric character"), though it's easier to test for the negation:
/(?<=\p{Alpha}cat(?<=\p{Alnum})/
The test passes if the string does not match this regex.
With interpretation #2, the regex is:
/(?<!\p{Alpha}cat(?!\p{Alnum})/
I think this will work:
/[^a-z0-9]?myspesificword[^a-z0-9]?/i
I want a regex that allows letters, number and a dash, which is ([a-z0-9\-]+), but I don't want one or more dashes on its own without a letter(s) or a number(s)
Is it possible?
--- Invalid
- Invalid
3e-qw Valid
-3- Valid
-a- Valid
By use of a word boundary:
/^-*\b[a-z\d-]*$/i
demo at regex101
Or requiring one letter/digit:
/^-*[a-z\d][a-z\d-]*$/i
demo at regex101
Or use of a negative lookahead to prevent matching strings only consisting of dashes:
/^(?!-+$)[a-z\d-]+$/i
demo at regex101
Use the following pattern:
[a-zA-Z0-9-]*(?=[a-zA-Z0-9])[a-zA-Z0-9-]*
https://regex101.com/r/E1yHVY/17
Right now I'm working on a client-side password validator that ensures that has:
8 characters
An uppercase letter
A lowercase letter
One number
I now need to exclude two specific characters, + and &. I wasn't exactly sure where to insert this rule in my current regular expression. What would be the best approach based on what I have now?
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/
Match anything except +, & and \n
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[^+&\n]{8,}$
Regex Demo
Since your example is lookahead based, it makes sense to now just add a negative lookahead to preclude & or +:
/^(?!.*(\+|&))(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/
Demo Here
You don't need to exclude characters + and &. You just need to mention what you want to match i.e [A-Za-z0-9]
Regex: ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{8,}$
Regex101 Demo
I'm using the following regular expression to match one or more special characters for a password strength test.
if (password.match(/\W+/)) points++;
This doesn't seem to match the underscore '_' as a special character. Why is this and how can I fix it?
It is because \W is the same as [^\w], while \w contains a-z, A-Z, 0-9, and _ as well.
In order to fix it just add _ character separately:
if (password.match(/[\W_]+/)) points++;
\W (uppercase) means not \w, so anything except word characters.
Word characters (\w) includes letters, digits, and underscore.
Perhaps you should use /[^a-z0-9]+/i to match non-letters.
Are you sure you don't want the \w? The \W is the negation of \w.
\w matches (letters, digits, and underscores), so \W does NOT match letters, digits, and underscores. See here: http://www.regular-expressions.info/reference.html
The match fails because underscore is treated as a word character. From the MDN documentation for \W:
Matches any non-word character. Equivalent to [^A-Za-z0-9_]
You can fix this by grouping underscore and \W:
if (password.match(/[\W_]+/)) points++;
A regex tool such as Javascript Regex Tester can be especially helpful for debugging this sort of thing.