rahul
rahul

Reputation: 7663

Regex for allowing alphanumeric,-,_ and space

I am searching for a regular expression for allowing alphanumeric characters, -, _ or spaces in JavaScript/jQuery.

Upvotes: 69

Views: 245052

Answers (10)

Vikash Kamat
Vikash Kamat

Reputation: 1

var1 regex = new     RegExp1("^[A-Za-z0-9? ,_-]+$");
var1 key = String1.fromCharCode(event1.charCode ? event1.which : event1.charCode);

if (!regex1.test(key)) {
  event1.preventDefault();
  return false;   
}

Upvotes: 0

israr comsian
israr comsian

Reputation: 1

Try this for allowing alphanumeric and space

[a-zA-Z0-9 ]

Upvotes: -1

Dinushika Rathnayake
Dinushika Rathnayake

Reputation: 419

Following both are worked for me.

/[^a-zA-Z0-9\-_\s]/g
/[^a-z\d\-_\s]+$/i

Can replace other except alphanumerics, -, _ with empty like this.

inputString.replace(/[^a-zA-Z0-9\-_\s]/g,"");

Upvotes: -1

kva
kva

Reputation: 496

Try this regex

*Updated regex

/^[a-z0-9]+([-_\s]{1}[a-z0-9]+)*$/i

This will allow only single space or - or _ between the text

Ex: this-some abc123_regex

To learn : https://regexr.com

*Note: I have updated the regex based on Toto question

Upvotes: 4

Sibeesh Venu
Sibeesh Venu

Reputation: 21719

For me I wanted a regex which supports a strings as preceding. Basically, the motive is to support some foreign countries postal format as it should be an alphanumeric with spaces allowed.

  1. ABC123
  2. ABC 123
  3. ABC123(space)
  4. ABC 123 (space)

So I ended up by writing custom regex as below.

/^([a-z]+[\s]*[0-9]+[\s]*)+$/i

enter image description here

Here, I gave * in [\s]* as it is not mandatory to have a space. A postal code may or may not contains space in my case.

Upvotes: 0

Asif Khan
Asif Khan

Reputation: 71

var regex = new RegExp("^[A-Za-z0-9? ,_-]+$");
            var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
            if (!regex.test(key)) {
                event.preventDefault();
                return false;
            }

in the regExp [A-Za-z0-9?spaceHere,_-] there is a literal space after the question mark '?'. This matches space. Others like /^[-\w\s]+$/ and /^[a-z\d\-_\s]+$/i were not working for me.

Upvotes: 4

Nevir
Nevir

Reputation: 8101

Character sets will help out a ton here. You want to create a matching set for the characters that you want to validate:

  • You can match alphanumeric with a \w, which is the same as [A-Za-z0-9_] in JavaScript (other languages can differ).
  • That leaves - and spaces, which can be combined into a matching set such as [\w\- ]. However, you may want to consider using \s instead of just the space character (\s also matches tabs, and other forms of whitespace)
    • Note that I'm escaping - as \- so that the regex engine doesn't confuse it with a character range like A-Z
  • Last up, you probably want to ensure that the entire string matches by anchoring the start and end via ^ and $

The full regex you're probably looking for is:

/^[\w\-\s]+$/

(Note that the + indicates that there must be at least one character for it to match; use a * instead, if a zero-length string is also ok)

Finally, http://www.regular-expressions.info/ is an awesome reference


Bonus Points: This regex does not match non-ASCII alphas. Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that.

For languages/platforms that do support named character sets, you can use /^[\p{Letter}\d\_\-\s]+$/

Upvotes: 112

cbayram
cbayram

Reputation: 2259

/^[-\w\s]+$/

\w matches letters, digits, and underscores

\s matches spaces, tabs, and line breaks

- matches the hyphen (if you have hyphen in your character set example [a-z], be sure to place the hyphen at the beginning like so [-a-z])

Upvotes: 12

elclanrs
elclanrs

Reputation: 94101

Try this regex:

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

Upvotes: 73

nkamm
nkamm

Reputation: 627

var string = 'test- _ 0Test';
string.match(/^[-_ a-zA-Z0-9]+$/)

Upvotes: 10

Related Questions