允许字母数字、-、_和空格的正则表达式。

时间:2021-08-08 21:45:28

hi all i am searching for regular expression for allowing alphanumeric,-,_ or space in javascript/jquery. i tired googling but didn't able to find that

hi,我正在寻找允许字母数字、-、_或空间在javascript/jquery中的正则表达式。我累了,但没能找到。

can any one help me out with this.

有人能帮我解决这个问题吗?

Thanks in advance.

提前谢谢。

6 个解决方案

#1


34  

Try this regex:

试试这个正则表达式:

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

#2


57  

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).
  • 您可以将alphanumeric与\w匹配,这与JavaScript(其他语言可能不同)相同。
  • 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
    • 注意,我正在转义——作为\-这样,regex引擎不会将它与像a - z这样的字符范围混淆。
  • 这些叶子和空间可以组合成一个匹配的集合,比如[\w\-]。但是,您可能需要考虑使用\s,而不是仅使用空格字符(\s也匹配制表符和其他形式的空白),注意我正在转义—作为\-以便regex引擎不会将它与像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

最后,http://www.regular-expressions.info/是一个很棒的参考。


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.

额外点数:这个正则表达式不匹配非ascii阿尔法值。不幸的是,大多数浏览器中的regex引擎不支持命名字符集,但是有一些库可以帮助它。

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

语言/平台,支持指定字符集,您可以使用/ ^[\ p {信} \ d \ _ \ \ s]+ /美元

#3


8  

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

\w matches letters, digits, and underscores

\w匹配字母、数字和下划线。

\s matches spaces, tabs, and line breaks

\s匹配空格、制表符和换行符。

- 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])

-匹配连字符(如果您的字符集示例[a-z]中有连字符,请确保在开始时使用连字符[-a-z])

#4


6  

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

#5


2  

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.

在正则表达式[A-Za-z0-9 ?空格,_-]在问号后面有一个文字空间。这匹配空间。其他类似/ ^[- \ w \]+ $ / / ^[a - z \ d \ _ \ s]+美元/我没有为我工作。

#6


0  

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. ABC123
  3. ABC 123
  4. 美国广播公司(ABC)123
  5. ABC123(space)
  6. ABC123(空间)
  7. ABC 123 (space)
  8. 美国广播公司(ABC)123(空间)

So I ended up by writing custom regex as below.

因此,我以下面的方式编写了自定义regex。

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

允许字母数字、-、_和空格的正则表达式。

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.

在这里,我给* * *,因为它不是强制的空间。在我的情况下,邮政编码可能包含或不包含空格。

#1


34  

Try this regex:

试试这个正则表达式:

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

#2


57  

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).
  • 您可以将alphanumeric与\w匹配,这与JavaScript(其他语言可能不同)相同。
  • 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
    • 注意,我正在转义——作为\-这样,regex引擎不会将它与像a - z这样的字符范围混淆。
  • 这些叶子和空间可以组合成一个匹配的集合,比如[\w\-]。但是,您可能需要考虑使用\s,而不是仅使用空格字符(\s也匹配制表符和其他形式的空白),注意我正在转义—作为\-以便regex引擎不会将它与像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

最后,http://www.regular-expressions.info/是一个很棒的参考。


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.

额外点数:这个正则表达式不匹配非ascii阿尔法值。不幸的是,大多数浏览器中的regex引擎不支持命名字符集,但是有一些库可以帮助它。

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

语言/平台,支持指定字符集,您可以使用/ ^[\ p {信} \ d \ _ \ \ s]+ /美元

#3


8  

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

\w matches letters, digits, and underscores

\w匹配字母、数字和下划线。

\s matches spaces, tabs, and line breaks

\s匹配空格、制表符和换行符。

- 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])

-匹配连字符(如果您的字符集示例[a-z]中有连字符,请确保在开始时使用连字符[-a-z])

#4


6  

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

#5


2  

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.

在正则表达式[A-Za-z0-9 ?空格,_-]在问号后面有一个文字空间。这匹配空间。其他类似/ ^[- \ w \]+ $ / / ^[a - z \ d \ _ \ s]+美元/我没有为我工作。

#6


0  

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. ABC123
  3. ABC 123
  4. 美国广播公司(ABC)123
  5. ABC123(space)
  6. ABC123(空间)
  7. ABC 123 (space)
  8. 美国广播公司(ABC)123(空间)

So I ended up by writing custom regex as below.

因此,我以下面的方式编写了自定义regex。

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

允许字母数字、-、_和空格的正则表达式。

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.

在这里,我给* * *,因为它不是强制的空间。在我的情况下,邮政编码可能包含或不包含空格。