简体   繁体   English

JavaScript中的正则表达式,字符存在两种类型

[英]Regular expression in javascript, existence of character two types

Here is a regular expression in JavaScript to check for the existence of characters from A to Z both lower and uppercase and 0 to 9 in a string. 这是JavaScript中的正则表达式,用于检查字符串中从A到Z的大小写和0到9的字符是否存在。

var validChars = /[0-9a-zA-Z]/;

In addition to these characters I wanted to check for the existence of the "@" sign and the forward slash "/" in the same string. 除了这些字符外,我还想检查同一字符串中是否存在"@"符号和正斜杠"/"

For example, it will check for the existence of characters 0 to 9 and A to Z and @ and / in a string. 例如,它将检查字符串中是否存在字符0到9和A到Z以及@/

How can this be done? 如何才能做到这一点?

Try this regex: 试试这个正则表达式:

[a-zA-Z0-9@\/]*

Explanation 说明

a-zA-Z: lower or upper case characters
0-9: numbers from 0 to 9
@: @
\/: /
*: zero or more machting. You can replace it with `+` for: one or more matching

Live demo 现场演示

You can simply add those characters to your class. 您可以简单地将这些字符添加到您的班级中。

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

Note : You should probably use beginning of string ^ and end of string $ anchors here, following your character class with a quantifier and the i modifier can be used for case insensitive matching. 注意 :您可能应该在这里使用字符串^开始和字符串$结尾,在字符类后面加上一个量词 ,并且i修饰符可用于不区分大小写的匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM