Regex for JavaScript to Allow Only Alphanumeric

RegEx for Javascript to allow only alphanumeric

/^[a-z0-9]+$/i

^ Start of string
[a-z0-9] a or b or c or ... z or 0 or 1 or ... 9
+ one or more times (change to * to allow empty string)
$ end of string
/i case-insensitive

Update (supporting universal characters)

if you need to this regexp supports universal character you can find list of unicode characters here.

for example: /^([a-zA-Z0-9\u0600-\u06FF\u0660-\u0669\u06F0-\u06F9 _.-]+)$/

this will support persian.

Regex for allowing alphanumeric,-,_ and space

Try this regex:

/^[a-z\d\-_\s]+$/i

Regex allowing only alphanumeric and special characters does not work

I see two major problems. One is that inside a string "...", backslashes \ have a special meaning, independent of their special meaning inside a regex. In particular, \d ends up just becoming d — not what you want. The best fix for that is to use the /.../ notation instead of new RegExp("..."):

var regPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]$/;

The other problem is that your regex doesn't match your requirements.

Actually, the requirements that you've stated don't really make sense, but I'm guessing you want something like this:

  1. Must contain at least one lowercase letter, at least one uppercase letter, at least one digit, and at least one of the special characters $@$!%*?&.
  2. Can only contain lowercase letters, uppercase letters, digits, and the special characters $@$!%*?&.
  3. Total length must be between 8 and 20 characters, inclusive.

If so, then you've managed #1 and #2, but forgot about #3. Right now your regex demands that the length be exactly 1. To fix this, you need to add {8,20} after the [A-Za-z\d$@$!%*?&] part:

var regPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,20}$/;

regex for allowing alphanumeric, special characters and not ending with @ or _ or

Your pattern allows at least 3 characters, where the last 3 are negated character classes matching any char other than the listed.

The pattern ^[a-zA-Z0-9._@]*[^_][^.][^@]$ will match 3 newlines, and adding all the chars to a single character class ^[a-zA-Z0-9._@]*[^@._]$ will also match a single newline only.


If you want to allow all 3 "special" characters and match at least 3 characters in total you can repeat the character class 2 or more times using {2,} and match a single char at the end without the special characters.

^[a-zA-Z0-9._@]{2,}[a-zA-Z0-9]$

Regex demo

Matching as least a single char (and not end with . _ @)

^[a-zA-Z0-9._@]*[a-zA-Z0-9]$

Regex demo

Regex for a string which do not start with a number and allow only alphanumeric

You may use the following regex:

^[A-Za-z]\w*$

Details

  • ^ - start of string
  • [A-Za-z] - any ASCII letter
  • \w* - zero or more letters/digits/_
  • $ - end of string.

To allow an empty string match, wrap the whole pattern with an optional non-capturing group:

^(?:[A-Za-z]\w*)?$
^^^ ^^

Regex to allow alphanumeric, spaces, some special characters

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.

Regex for containing only alphanumeric characters and underscores, start with an alphabetic character

Use

^[A-Za-z][A-Za-z0-9_]{0,39}$

See regex proof.

Synonyms:

^[A-Za-z]\w{0,39}$
^\p{L}\w{0,39}$
^[[:alpha:]]\w{0,39}$

EXPLANATION

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[A-Za-z] any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
[A-Za-z0-9_]{0,39} any character of: 'A' to 'Z', 'a' to 'z',
'0' to '9', '_' (between 0 and 39 times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

Allow only alphanumeric characters or empty spaces with Regex

It is because part after | doesn't have assertions of anchors ^ and $.

You can use:

^$|^[a-zA-Z0-9]+$

Or:

^[a-zA-Z0-9]*$

Or:

^(?:[a-zA-Z0-9]+)?$

They will all be doing same thing.



Related Topics



Leave a reply



Submit