[英]Regex which should not allow any special characters except ,
I am trying to create a regular expression which does not allow any special characters except ,
, .
我正在尝试创建一个正则表达式,该表达式除,
之外不允许任何特殊字符.
and they should not come side by side. 他们不应该并排。
For example: STax.sdn,skm
should be accepted whereas SDs,.Hnj
should throw an error message. 例如:应该接受STax.sdn,skm
而SDs,.Hnj
应该抛出错误消息。 I have used the below code, however it is accepting ,
and .
我已用下面的代码,然而,接受,
和.
side by side which I don't want. 并排,我不想。
function validateAnnouncementTags(){
var announcementTags = document.getElementById("announcementTags").value;
if (announcementTags.search(/[<>'+\"\/`\\\[\]^={}%;@#!$&*()?:|]/)>-1 ) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
使用此模式:
/^(?!.*[\.,])/
Based on your comments, I am assuming that you want to accept any letters separated by periods or commas. 根据您的评论,我假设您想接受任何以句点或逗号分隔的字母。 How about we: 我们呢:
we can use 我们可以用
function validateAnnouncementTags() {
var announcementTags=document.getElementById("announcementTags").value;
if (announcementTags.match(/[a-zA-Z\.,]*/)[0] != annoucementTags
|| announcementTags.search(/[\.,][\.,]/) >= 0
) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
But, if I may be so bold as to assume more structure to your acceptable syntax: 但是,如果我可能这么大胆,以使您的可接受语法具有更多结构:
Then we can use: 然后我们可以使用:
function validateAnnouncementTags() {
var announcementTags=document.getElementById("announcementTags").value;
if (announcementTags.match(/([a-z0-9]+[\.,]?)*/)[0] != annoucementTags ) {
$('#announcementTagsSpecialCharError').addClass('show');
} else {
$('#announcementTagsSpecialCharError').addClass('hide');
$('#announcementTagsSpecialCharError').removeClass('show');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.