【问题标题】:RegEx for checking character X places after another characterRegEx 用于检查另一个字符之后的字符 X 位置
【发布时间】:2019-10-05 14:36:38
【问题描述】:

我已经完成了一个非常基本的步骤,它检查是否存在一个特殊字符。下一步需要一些建议,因为我希望能够在找到 # 后从 1 个位置开始搜索另一个特殊字符。

var reg = /#/;
alert(reg.test(string))

例如:

abc#.123     //invalid - as . is immediately after #
abc#12.3     //valid  - as . is more than 1 character after #
abc#abcd.23  //valid - as . is more than 1 character after #
a#123.12     //valid - as . is more than 1 character after #
a#12123124.12 //valid - as . is more than 1 character after #
abcd#1231.09  //valid - as . is more than 1 character after #
1.23#12312.01 //invalid - as . is before #
123#122.01#12 //invalid - as there is another# after .

因此#. 之间的间隔应始终为1 个或多个字符,# 始终排在第一位。

【问题讨论】:

标签: javascript regex regex-negation regex-lookarounds regex-greedy


【解决方案1】:

您可以断言字符串 ^ 的开头,不匹配 #. 使用 negated character class [^#.],然后匹配 #

然后重复该部分,然后是点,然后重复该部分直到字符串的末尾:

^[^#.]*#[^.#]+\.[^.#]*$

Regex demo

说明

  • ^ 字符串开始
  • [^#.]*# 匹配 0+ 次而不是 #. 然后匹配 #
  • [^.#]+\. 匹配 1+ 次而不是 .# 然后匹配一个点
  • [^.#]* 匹配 0+ 次而不是 .#
  • $ 字符串结束

let pattern = /^[^#.]*#[^.#]+\.[^.#]*$/;
[
  "abc#.123",
  "abc#12.3",
  "abc#abcd.23",
  "a#123.12",
  "a#12123124.12",
  "abcd#1231.09",
  "1.23#12312.01",
  "123#122.01#12"
].forEach(s => console.log(s + ": " + pattern.test(s)))

【讨论】:

  • 您的正则表达式匹配#..aa,其中#. 之间没有间隙,因此不应该匹配。
  • 这就是第二种模式。你说and hence not match,但在问题中并没有说明间隙中不能有点,而是I want to be able to search for another special character starting from 1 places after finding #
  • 每个人都是对的!从原始帖子中完全不清楚应该如何解释。
  • 好吧,OP 在这里试图说明的要点是,. 不应该紧跟在 # 之后,因此这篇文章,但我会让 OP 澄清一下这个。
  • 用场景更新了问题,希望这会更有意义。
【解决方案2】:

您可以使用/^[^\.#]*#[^\.#]+\.[^\.#]*$/

^  beginning of line anchor
 [^\.#]*  zero or more characters other than . and #
        #  literal # character
         [^\.#]+  one or more characters other than . and #
                \.  literal . character
                  [^\.#]*  one or more characters other than . and #
                         $  EOL

一般来说,如果您想要一个特定大小的最小间隙(在本例中为 5 或更大),请使用 /^[^\.#]*#[^\.#]{5,}\.[^#\.]*$/,如果您希望间隙正好为 5,请使用 {5}

var reg = /^[^\.#]*#[^\.#]+\.[^\.#]*$/;

[
  "abc#.123",      // invalid - as . is immediately after #
  "abc#12.3",      // valid - as . is more than 1 character after #
  "abc#abcd.23",   // valid - as . is more than 1 character after #
  "a#123.12",      // valid - as . is more than 1 character after #
  "a#12123124.12", // valid - as . is more than 1 character after #
  "abcd#1231.09",  // valid - as . is more than 1 character after #
  "1.23#12312.01", // invalid - as . is before #
  "123#122.01#12", // invalid - as there is another# after .
].forEach(test => console.log(reg.test(test)));

【讨论】:

  • 这是匹配abc#..
  • 感谢您的注意——OP 更新了要求,直到现在我还没有看到任何关于它的消息。有关先前的歧义,请参阅第四只鸟的答案中的讨论。
【解决方案3】:

您可以使用此正则表达式来确保字符串中的第一个 # 后跟一个点 .,其中 #. 之间的间隔是一个或多个字符,

^[^#]*#[^.]+\.

说明:

  • ^ - 字符串开始
  • [^#]* - 除# 之外的零个或多个字符,如果您希望点也不会出现在# 之前,那么您可以将其更改为[^#.]*
  • # - 匹配第一个 # 字符
  • [^.]+ - 匹配一个或多个除点以外的字符
  • \. - 匹配文字点

Regex Demo

JS 代码演示,

const arr = ['abc#.123','abc#12.3','abc#abcd.23','a#123.12','a#..dd']

arr.forEach(s => console.log(s + " --> " + /^[^#]*#[^.]+\./.test(s)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2020-04-22
    • 2019-09-29
    相关资源
    最近更新 更多