简体   繁体   English

正则表达式,除,,

[英]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,skmSDs,.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: 我们呢:

  1. Check for valid characters, and 检查有效字符,并
  2. Ensure that no "special" chars occur adjacent? 确保没有相邻的“特殊”字符?

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: 但是,如果我可能这么大胆,以使您的可接受语法具有更多结构:

  1. Accept any sequence of letters separated by a comma or period 接受以逗号或句点分隔的任何字母序列
  2. The sequence will not start with a comma or period 该序列不会以逗号或句号开头
  3. The sequence can end with a comma or period 序列可以以逗号或句号结尾

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.

相关问题 不允许使用特殊字符,但允许的字符除外javascript regex - Do not allow special characters except the allowed characters javascript regex 正则表达式允许特殊字符 - Regex to allow special characters 到期日期不允许使用特殊字符,但/ - Not allow special characters in expiration date except / RegEx 模式不允许除下划线外的特殊字符 - RegEx pattern to not allow special character except underscore 正则表达式不允许在字符串开始或结束处使用“.”、“_”、“-”,并且不应有连续的“.” 其余的所有特殊字符都不应被允许 - Regex to not allow '.', '_', '-' at String Start or End and should not have consecutive '.' and the rest all special characters should not be allowed 正则表达式删除除数字以外的所有特殊字符? - Regex remove all special characters except numbers? 正则表达式删除文件名中的特殊字符(扩展名除外) - Regex remove special characters in filename except extension 正则表达式只允许 az 中的任何字符,包括小写和大写、任意数字和特殊字符。 -_ 只要 - Regex to allow only any character from a-z both lower and upper, any number and special characters . -_ only 正则表达式允许某些特殊字符-转义问题 - Regex to allow certain special characters - escape issue 正则表达式允许使用字母数字,空格,一些特殊字符 - Regex to allow alphanumeric, spaces, some special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM