正则表达式过滤掉 "n" 字符的出现
Regex to filter out "n" occurence of character
我需要一个正则表达式(用于输入验证)来过滤除 "underscore" 以外的所有特殊字符,因此允许 [a-zA-Z0-9\underscore] 范围内的所有字符并且可以出现多次.但是在我的表达式中,我不能连续出现 2 次 "underscore",而且我的字符串不能以“_”开头。
有时您会发现颠倒逻辑——发现任何问题,而不是确保没有问题——会产生更简单的解决方案:
// starts with underscore
// or has two underscores in a row
// or has a character other than alpha/numeric/underscore
var bad = /^_|_{2}|\W/;
if (bad.test(input)) myInputIsIllegal();
我想你想使用groups so that the repetitions正常工作:
/[A-Za-z0-9]+(?:_(?:[A-Za-z0-9]+|$))*/
/^([a-zA-Z0-9]_?)+$/
一个或多个...字母数字字符(可选)后跟单个下划线
我需要一个正则表达式(用于输入验证)来过滤除 "underscore" 以外的所有特殊字符,因此允许 [a-zA-Z0-9\underscore] 范围内的所有字符并且可以出现多次.但是在我的表达式中,我不能连续出现 2 次 "underscore",而且我的字符串不能以“_”开头。
有时您会发现颠倒逻辑——发现任何问题,而不是确保没有问题——会产生更简单的解决方案:
// starts with underscore
// or has two underscores in a row
// or has a character other than alpha/numeric/underscore
var bad = /^_|_{2}|\W/;
if (bad.test(input)) myInputIsIllegal();
我想你想使用groups so that the repetitions正常工作:
/[A-Za-z0-9]+(?:_(?:[A-Za-z0-9]+|$))*/
/^([a-zA-Z0-9]_?)+$/
一个或多个...字母数字字符(可选)后跟单个下划线