Javascript javascript正则表达式验证带空格的字母数字文本并拒绝特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8536187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:24:30  来源:igfitidea点击:

javascript regex to validate alphanumeric text with spaces and reject special characters

javascriptregexvalidation

提问by optimusprime619

I'm not an expert with regex but tried my hand with validating a field that allows alphanumeric data with spaces but not any other special characters. Where linkTitle is the name of the variable to test i tried with the following regex in my conditional

我不是正则表达式专家,但我尝试验证一个字段,该字段允许带有空格的字母数字数据但不允许任何其他特殊字符。其中 linkTitle 是要测试的变量的名称,我在我的条件中尝试使用以下正则表达式

/[^A-Za-z\d\s]/.test(linkTitle)
/[^A-Za-z0-9\s]/.test(linkTitle)
/[^A-Za-z\d ]/.test(linkTitle)

and none of these worked... i'm curious to know what went wrong with the regex using \s which seemingly refers whitespaces and what would be the apt regex to fit the bill.

并且这些都不起作用......我很想知道使用 \s 的正则表达式出了什么问题,它似乎指的是空格,以及适合该法案的正则表达式是什么。

Thanks in advance!!

提前致谢!!

回答by Adam Rackis

I think you want to match the beginning of the string once, then use the Positive Closure—one or more—of your letters, spaces or digits, then the end of the string.

我认为您想匹配字符串的开头一次,然后使用字母、空格或数字的一个或多个正闭包,然后是字符串的结尾。

/^[A-Za-z\d\s]+$/.test(linkTitle)

Tested with:

测试:

var reg = /^[A-Za-z\d\s]+$/;
["Adam", "Ada m", "A1dam", "A!dam", 'sdasd 213123&*&*&'].forEach(function (str) {
    console.log(reg.test(str + "\n"));
});

Shows true, true, true, false, false

显示true, true, true, false,false



Or if you want to allow empty strings, you could use the same RegEx but with the Kleene Closure—zero or more—of letters, digits or spaces

或者,如果您想允许空字符串,您可以使用相同的 RegEx,但使用 Kleene 闭包(零个或多个)字母、数字或空格

var regAllowEmpty = /^[A-Za-z\d\s]*$/;
["", "Adam", "Ada m", "A1dam", "A!dam", 'sdasd 213123&*&*&'].forEach(function (str) {
    console.log(regAllowEmpty.test(str + "\n"));
}); 

note that forEachwill not work on older browsers - this is just for testing

请注意,forEach这不适用于较旧的浏览器 - 这仅用于测试

回答by optimusprime619

Any of your three regex here will match any single character that is notone between [and ]excluding ^of course.

此处的三个正则表达式中的任何一个都将匹配当然不是介于[]不包括之间的任何单个字符^

The problem maybe comes from the way you interpret the result provided by test(). Here if the regex matches the string linkTitle, and testreturns true; this means you got a wrong char in the input (neither a capital letter, nor in lower case, not a digit and not a space).

问题可能来自您解释test(). 这里如果正则表达式匹配 string linkTitle,则test返回 true;这意味着您在输入中输入了错误的字符(既不是大写字母,也不是小写字母,不是数字也不是空格)。

Have tested your regexs :

已测试您的正则表达式:

/[^A-Za-z\d\s]/.test('0 '); // no match, false, input is ok
/[^A-Za-z\d\s]/.test('0 $'); // match, true, input is wrong