繁体   English   中英

用于完全阻止特殊字符的正则表达式模式,并阻止空格开始和结束字符串,并允许在 javascript 中的单词之间

[英]regEx pattern for blocking special characters fully, and blocking spaces start and end of the sting and allow in between words in javascript

用于完全阻止特殊字符的正则表达式模式,并阻止空格开始和结束字符串,并允许在 JavaScript 中的单词之间,

我已经尝试了很多方法,但我无法得到它。

要求:字符串前后不能有空格,不能有特殊字符( <>?/|\:"*+ )。

我试过这个^[^<>?/+:"|\\\\*+\\s][^<>?+:"|\\\\*+]*$

这是处理字符串后除空格之外的所有内容

花了一些时间来确定,通常我会把它分成两个检查(一个用于特殊字符,另一个用于前导和尾随空格),或者只是修剪前导和尾随空格并只测试特殊字符。

/^(?!\s)[^<>?/|\\:"*+]*[^<>?/|\\:"*+\s]$/
  • ^(?!\s)不以非空白字符开头
  • [^<>?/|\\:"*+\s]$非特殊/以非空白字符结尾
  • [^<>?/|\\:"*+]*不包含任何特殊字符

测试套件

const regex = /^(?!\s)[^<>?/|\\:"*+]*[^<>?/|\\:"*+\s]$/;

it.each([
  // valid - single word
  ["t", true],
  ["te", true],
  ["tes", true],
  ["test", true],
  // valid - multi-word
  ["test test", true],
  ["test test test", true],
  [
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel vulputate lacus, bibendum auctor nulla. Cras eget vehicula arcu. Aliquam non lacus sed quam consequat laoreet sed at massa. Nam magna nulla, tempus id consequat efficitur, luctus non sapien. Maecenas eget volutpat neque. Etiam lobortis ipsum a imperdiet semper. Sed vel quam porta, lobortis metus commodo, aliquam libero. Vestibulum nulla tellus, sagittis quis auctor id, malesuada ac purus. Phasellus nunc mi, viverra id sem ut, laoreet porta orci. Cras malesuada quam ut auctor tristique. Suspendisse bibendum purus tortor, nec finibus magna imperdiet ut. Mauris id ligula dictum, feugiat nulla non, bibendum nunc. Phasellus eget sem cursus, placerat risus non, vehicula quam.",
    true
  ],
  // invalid - leading/trailing whitespace
  ["", false],
  ["  test", false],
  ["test  ", false],
  ["  test  ", false],
  ["  test test  ", false],
  // invalid - special chars only
  ["<", false],
  [">", false],
  ["?", false],
  ["/", false],
  ["|", false],
  ["\\", false],
  [":", false],
  ['"', false],
  ["*", false],
  ["+", false],
  // invalid - starts with special chars
  ["<t", false],
  ["<te", false],
  ["<tes", false],
  ["<test", false],
  [">test", false],
  ["?test", false],
  ["/test", false],
  ["|test", false],
  ["\\test", false],
  [":test", false],
  ['"test', false],
  ["*test", false],
  ["+test", false],
  // invalid - ends with special chars
  ["t<", false],
  ["st<", false],
  ["est<", false],
  ["test<", false],
  ["test>", false],
  ["test?", false],
  ["test/", false],
  ["test|", false],
  ["test\\", false],
  ["test:", false],
  ['test"', false],
  ["test*", false],
  ["test+", false],
  // invalid - special chars mixed
  ["te<st", false],
  ["te>st", false],
  ["te?st", false],
  ["te/st", false],
  ["te|st", false],
  ["te\\st", false],
  ["te:st", false],
  ['te"st', false],
  ["te*st", false],
  ["te+st", false],
  // invalid - leading/trailing and special chars mixed
  ["  te<st", false],
  ["te<st  ", false],
  ["  te<st  ", false],
  // invalid - multi-word, leading/trailing whitespace, and special chars
  ["te?st test", false],
  ["test te?st", false],
  ["test ? test", false],
  ["  te?st test", false],
  ["test te?st  ", false],
  ["  test ? test  ", false]
])("should validate [%s] - %s", (input, expected) => {
  expect(regex.test(input)).toEqual(expected);
});

编辑正则表达式 - 没有特殊字符和前导/尾随空格,允许单词

试试这个模式^(?:\s)[^<>/:"\\|+*\n]*[^<>/:"\\|+*\s]$

它匹配您提供的所有测试模式。 字符类中只有一点重复,但那是因为最后我们需要确保它不会以空白字符结尾。

在 regex101 链接中有对模式片段的解释。

正则表达式101

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM