【问题标题】:How to restrict special characters at the start or end in string using regex pattern?如何使用正则表达式模式限制字符串开头或结尾的特殊字符?
【发布时间】:2020-10-14 21:26:33
【问题描述】:

我需要创建一个具有以下条件的正则表达式模式-

  1. 它可以有 %[百分比]、/[正斜杠]、\[反斜杠]、( ) [括号]、™ [商标]、+[加号]、-[连字符]、:[冒号]。可以是单词、特殊字符和数字的组合。
  2. 它可以允许 check-in 之类的词,但不应允许 - :-% : 之类的词,基本上如果有人尝试写的话以特殊字符开头的东西是不允许的,但提到的字符可以介于两者之间。
  3. 字符串之间也不能有两个特殊字符。
  4. 它还可以在字符串的开头、中间和结尾之间允许空格。
  5. 它也可以接受单个字符。

我创建了一个不允许其他特殊字符,除了提到的字符和空格。我如何允许在两者之间而不是在开头提到的字符。以下是我写的:

var string='Incharge';
var a= RegExp(/^[a-zA-Z0-9-:%/\\()\u2122.+\s]+$/).test(string);
console.log(a);

我的要求:

string='In-charge' -> Correct
string='Incharge' -> Correct
string='In charge' -> Correct
string='-In-charge' -> In Correct
string='--In-charge' -> In Correct
string='  In-charge' -> Correct
string=' a' -> Correct
string='in-charge' -> Correct
string='in-:charge' -> In Correct
string=' in-  ' -> Correct
string='in@charge' -> In Correct

我试图适应所有条件,但无法做到。有人可以帮我吗?

【问题讨论】:

  • 您不必滥用标签。
  • Java和javascript没有关系。请选择一个。
  • 您好,为错误道歉。删除了错误的标签。
  • 我已经编辑了这个问题,使其更加详细和相关。我怎么知道它是否会被允许回答而不是关闭?有人可以帮我吗?
  • 试试^[A-Za-z0-9 ]+(?:[%\/\\()\u2122+\-:][A-Za-z0-9 ]+)*$Demo

标签: regex validation


【解决方案1】:

使用

/^[a-zA-Z0-9\s]+(?:[-:%/\\()\u2122.+][a-zA-Z0-9\s]+)*$/

proof

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-zA-Z0-9\s]+           any character of: 'a' to 'z', 'A' to 'Z',
                           '0' to '9', whitespace (\n, \r, \t, \f,
                           and " ") (1 or more times (matching the
                           most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [-:%/\\()\u2122.+]       any character of: '-', ':', '%', '/',
                             '\\', '(', ')', 'TM', '.', '+'
--------------------------------------------------------------------------------
    [a-zA-Z0-9\s]+           any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9', whitespace (\n, \r, \t,
                             \f, and " ") (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

JavaScript:

const valid_strings = ['In-charge','Incharge','In charge','  In-charge',' a','in-charge',' in-  '];
const invalid_strings = ['-In-charge','--In-charge','in-:charge','in@charge '];
const regex = /^[a-zA-Z0-9\s]+(?:[-:%/\\()\u2122.+][a-zA-Z0-9\s]+)*$/;
valid_strings.forEach(x => console.log(x, '(must be true) =>', regex.test(x)));
invalid_strings.forEach(x => console.log(x, '(must be false) =>', regex.test(x)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 2013-08-04
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2023-01-02
    相关资源
    最近更新 更多