admin管理员组文章数量:1391924
i tried to make a regex for my Address column the code is:
var str = "97sadf []#-.'";
var regx = /^[a-zA-z0-9\x|]|[|-|'|.]*$/;
if(str.match(regx))
document.write('Correct!');
else
document.write('Incorrect!');
the special character i want that is ][#-.
the given code return me the correct match but if i add another kind of the special character like @%
then the correct result i got, but i want the incorrect result.
i don't know where i did wrong please help me to make right..
EDIT: Sorry guys but the one thing i have to discuss with you that is there is no necessary to enter the special characters that i mentioned ][#-.
, but if the someone enter other then the given special character then should return the incorrect.
i tried to make a regex for my Address column the code is:
var str = "97sadf []#-.'";
var regx = /^[a-zA-z0-9\x|]|[|-|'|.]*$/;
if(str.match(regx))
document.write('Correct!');
else
document.write('Incorrect!');
the special character i want that is ][#-.
the given code return me the correct match but if i add another kind of the special character like @%
then the correct result i got, but i want the incorrect result.
i don't know where i did wrong please help me to make right..
EDIT: Sorry guys but the one thing i have to discuss with you that is there is no necessary to enter the special characters that i mentioned ][#-.
, but if the someone enter other then the given special character then should return the incorrect.
1 Answer
Reset to default 3The correct regex (assuming you want uppercase letters, lowercase letters, numbers, spaces and special characters [].-#'
) is:
var regx = /^[a-zA-Z0-9\s\[\]\.\-#']*$/
There are a couple things breaking your code.
First, [
, ]
, -
and .
have special meaning, and must be escaped (prefixed with \
).
\x
checks for line breaks, where we want spaces (\s
).
Next, lets look at the structure; for simplicity's sake, lets simplify to ^[abc]|[def]*$
. (abc
and def
being your two blocks of character types). Since the *
is attached to the second block, it is saying one instance of [abc]
or any number of [def]
.
Finally, we don't need |
inside of brackets, becuase they already mean one character contained within them (already behaves like an or).
本文标签: javascriptRegex for alphanumeric and special characters in jqueryStack Overflow
版权声明:本文标题:javascript - Regex for alphanumeric and special characters in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744688277a2619836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论