I have this ^[a-zA-Z0-9 #&$]*$, but not working for me in few cases.
If someone types
A string that only consists of digits (e.g. 1234567)
A string starting with a special character (e.g. &123abc)
need to be rejected. Note that a special char can be in the middle and at the end.
You seem to need to avoid matching strings that only consist of digits and make sure the strings start with an alphanumeric. I assume you also need to be able to match empty strings (the original regex matches empty strings).
That is why I suggest
^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)?$
See the regex demo
Details
^ - start of string
(?!\d+$) - the negative lookahead that fails the match if a string is numeric only
(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)? - an optional sequence of:
[a-zA-Z0-9] - a digit or a letter
[a-zA-Z0-9 #&$]* - 0+ digits, letters, spaces, #, & or $ chars
$ - end of string.
you can do it with the following regex
^(?!\d+$)\w+\S+
check the demo here
Related
I'm trying to create a regular expression that matches strings such as:
N1-112S
So far I have succeeded with the following (although I'm not really sure why it works):
item.match(/^\D.-/)
I'd like to further bolster the results by ensuring that the last character is A-Z as well.
I'd appreciate some help on a good regular expression for matching this pattern. Thanks!
If you plan to match a string that starts with an uppercase ASCII letter, then has a digit, then a hyphen, then 1 or more digits and then an ASCII letter at the end of the string use
/^[A-Z]\d-\d+[A-Z]$/.test(item)
See the regex demo. Also, to test if a regex matches some string or not, I'd recommend RegExp#test.
Pattern details
^ - start of string
[A-Z] - an uppercase ASCII letter
\d - an ASCII digit
- - a hyphen
\d+ - 1+ digits
[A-Z] - an ASCII letter
$ - end of string.
Variations
To match any alphanumeric chars after hyphen till the end of string, you need to change the above pattern a bit:
/^[A-Z]\d-[\dA-Z]*[A-Z]$/
The second \d+ is changed to [\dA-Z]*, any 0 or more ASCII digits or letters.
If there can be any chars after -, use .* or [^] instead of a \d+:
/^[A-Z]\d-.*[A-Z]$/
I have the following javascript regex:
/^[^\s][a-z0-9 ]+[^\s]$/i
I need to allow any alphanumeric character as well as spaces inside the string but not at the beginning nor at the end.
Oddly enough, the above regex will not accept less than 3 characters, e.g. aa will not match but aaa will.
I am not sure why. Can anyone please help ?
You have: [^\s] (requires matching at least one non-whitespace character), [a-z0-9 ]+ (requires matching at least one alphanumeric or space character), and [^\s] again (requires matching at least one non-whitespace character). So, in total, you need at least 3 characters in the string.
Use word boundaries at the beginning and end instead:
/^\b[a-z0-9 ]+\b$/i
https://regex101.com/r/2GhH3N/1
Try the following regex:
^(?! )[a-z0-9 ]*[a-z0-9]$
Details:
^(?! ) - Start of the string and no space after it (so here we exclude the
initial space).
[a-z0-9 ]* - A sequence of letters, digits and spaces, possibly empty
(the content before the last letter(see below).
[a-z0-9]$ - The last letter and the end of string (so here we exclude the
terminal space).
You should re-write the expression as
/^[a-z0-9]+(?:\s+[a-z0-9]+)*$/i
See the regex demo.
NOTE: If only one whitespace is allowed between the alphanumeric chars use
/^[a-z0-9]+(?:\s[a-z0-9]+)*$/i
^^
Details
^ - start of string
[a-z0-9]+ - 1+ letters/digits
(?:\s+[a-z0-9]+)* - 0 or more repetitions of 1+ whitespaces (\s+) and 1+ digit/letters
$ - end of string.
See the regex graph:
I have a special requirement, where i need the achieve the following
No Special Character is allowed except _ in between string.
string should not start or end with _, . and numeric value.
underscore should not be allowed before or after any numeric value.
I am able to achieve most of it, but my RegEx pattern is also allowing other special characters.
How can i modify the below RegEx pattern to not allow any special character apart from underscore that to in between strings.
^[^0-9._]*[a-zA-Z0-9_]*[^0-9._]$
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*\d_)(?!.*_\d)[a-zA-Z0-9_]+$
Explanation
^ Assert the start of the string
(?![0-9._]) Negative lookahead to assert that the string does not start with [0-9._]
(?!.*[0-9._]$) Negative lookahead to assert that the string does not end with [0-9._]
(?!.*\d_) Negative lookahead to assert that the string does not contain a digit followed by an underscore
(?!.*_\d) Negative lookahead to assert that the string does not contain an underscore followed by a digit
[a-zA-Z0-9_]+ Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a .
$ Assert the end of the string
Regex demo
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);
Your opening and closing sections; [^0-9._], say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
I need regular expression which could match a string like that
_test
123test
test
test_123
test-123
123.a
I created this regular expression:
/^[_0-9a-z][_.\-a-z0-9]*$/
However, I want to exclude a string if it only contains numbers.
How can I fix it?
To avoid matching a digit-only string, add a negative lookahead:
^(?![0-9]+$)[_0-9a-z][_.\-a-z0-9]*$
^^^^^^^^^^
The (?![0-9]+$) lookahead is triggered only once at the beginning of the string and will try to match one or more digits up to the end of string. If they are found, a match will be failed (no match will be returned) as the lookahead is negative.
I have this regex
/^[a-z]{1,}( (?=[a-z])){0,}(_(?=[a-z])){0,}[a-z]{0,}$/
I want to match
ag_b_cf_ajk
or
zva b c de
or
hh_b opxop a_b
so any character tokens separated by a single space or underscore.
(In the regex above, we have a literal space, which is legal, and we have look-aheads that ensure that a space or underscore is followed by a character).
The problem is, my above regex is only matching the first space or underscore, like so:
axz_be
axz be
but these fail
axz_be_j
axz be j
I believe I missing some concept with regexes in order to solve this as I have been trying for the last few hours!
It seems you can just use
^[a-z]+(?:[_ ][a-z]+)*$
See the regex demo
The regex matches
^ - start of string
[a-z]+ - one or more lowercase ASCII letters
(?:[_ ][a-z]+)* - zero or more sequences of:
[_ ] - a space or an underscore
[a-z]+ - one or more lowercase ASCII letters
$ - end of string
If the space or underscore must appear at least once, use the + quantifier instead of *:
^[a-z]+(?:[_ ][a-z]+)+$
^
To add a multicharacter alternative to the underscore and hyphen, you need to introduce another non-capturing group:
^[a-z]+(?:(?:[_ ]|\[])[a-z]+)+$
See another regex demo