我在javascript中应用正则表达式检查,以便用户可以输入。
var check=/^[\n\*\'\/.\+(),:_a-zA-Z0-9 @]*$/.test(body);
if (check != true)
{
alert("not allowed");
}
仅允许*()_ + @',。/特殊字符以及所有字母,数字和空格,但除了输入(\n
)之外,其工作正常。
答案 0 :(得分:0)
正则表达式中的美元符号表示行的结尾。删除它,你会很高兴。
答案 1 :(得分:0)
您可以使用这样的多行正则表达式:
^([\*\(\)\+\@',\.\/\w\s]|(\n|\r))*$
NODE EXPLANATION
^ the beginning of the string
( group and capture to \1 (0 or more times (matching the most amount possible)):
[\*\(\)\+\@',\.\/\ any character of: '\*', '\(', '\)', w\s] '\+', '\@', ''', ',', '\.', '\/', word characters (a-z, A-Z, 0-9, _), whitespace (\n, \r, \t, \f, and " ")
| OR
( group and capture to \2:
\n '\n' (newline)
| OR
\r '\r' (carriage return)
) end of \2
)* end of \1 (NOTE: because you are using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1)
$ before an optional \n, and the end of the string
var regex = /^([\*\(\)\+\@',\.\/\w\s]|(\n|\r))*$/
var check = regex.test(body);
if (check != true)
{
alert("not allowed");
}