Regex 匹配正则表达式进行规范化

Regex 匹配正则表达式进行规范化,regex,Regex,正则表达式非常糟糕,请在此寻求帮助 我正在编写的函数是接受输入,并返回规范化的输出 所以输入必须匹配特定的,而不是列出所有可能的组合,有什么方法可以使用RegExp吗 e、 g.jackjill | jack jill | jack&jill | jack&jill | jack-&jill | jack and jill | jack and jill | jack and jill | jack n jill |……,函数将返回“jack&jill” 因此,input.match(/jack

正则表达式非常糟糕,请在此寻求帮助 我正在编写的函数是接受输入,并返回规范化的输出 所以输入必须匹配特定的,而不是列出所有可能的组合,有什么方法可以使用RegExp吗

e、 g.jackjill | jack jill | jack&jill | jack&jill | jack-&jill | jack and jill | jack and jill | jack and jill | jack n jill |……,函数将返回“jack&jill”


因此,input.match(/jack[-&n]*jill/)覆盖了,-,n,但是“and”呢?

您可以使用可选字符类和使用
扩展模式

\bjack(?:[ &-]+|[ -]?(?:and|n)[ -]?)?jill\b

如果
n
之前和之后的内容应该相同,则可以对之前的零件使用捕获组,并使用反向引用匹配之后的相同内容

\bjack(?:[ &-]+|([ -]?)(?:and|n)\1)?jill\b
解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    jack                     'jack'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [\s&-]*                  any character of: whitespace (\n, \r, \t,
                           \f, and " "), '&', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      n                        'n'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      and                      'and'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    [\s&-]*                  any character of: whitespace (\n, \r,
                             \t, \f, and " "), '&', '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    jill                     'jill'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
  • \b确认
    单词边界,然后匹配杰克
  • (?:
    非捕获组
    • [&-]+
      匹配空格、
      &
      -
    • |
    • ([-]?)
      可以选择在捕获组1中匹配空格或
      -
    • (?:和| n)
      匹配
      n
    • \1
      反向引用组1中捕获的内容
  • )?
    关闭组并将ik设置为可选
  • jill\b
    匹配
    jill
    后跟单词边界

您可以使用可选字符类和使用
|

\bjack(?:[ &-]+|[ -]?(?:and|n)[ -]?)?jill\b

如果
n
之前和之后的内容应该相同,则可以对之前的零件使用捕获组,并使用反向引用匹配之后的相同内容

\bjack(?:[ &-]+|([ -]?)(?:and|n)\1)?jill\b
解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    jack                     'jack'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [\s&-]*                  any character of: whitespace (\n, \r, \t,
                           \f, and " "), '&', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      n                        'n'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      and                      'and'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    [\s&-]*                  any character of: whitespace (\n, \r,
                             \t, \f, and " "), '&', '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    jill                     'jill'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
  • \b确认
    单词边界,然后匹配杰克
  • (?:
    非捕获组
    • [&-]+
      匹配空格、
      &
      -
    • |
    • ([-]?)
      可以选择在捕获组1中匹配空格或
      -
    • (?:和| n)
      匹配
      n
    • \1
      反向引用组1中捕获的内容
  • )?
    关闭组并将ik设置为可选
  • jill\b
    匹配
    jill
    后跟单词边界

您可以用
\bjack.*?jill\b
替换
杰克[-&n]*jill

详细信息

  • *?
    :杰克和吉尔之间的任何信件
  • \b
    :单词边界

您可以用
\bjack.*?jill\b
替换
杰克[-&n]*jill

详细信息

  • *?
    :杰克和吉尔之间的任何信件
  • \b
    :单词边界
使用

replace(/\b(jack)[\s&-]*(?:(?:n|and)[\s&-]*)?(jill)\b/gi, '$1 & $2')

*解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    jack                     'jack'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [\s&-]*                  any character of: whitespace (\n, \r, \t,
                           \f, and " "), '&', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      n                        'n'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      and                      'and'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    [\s&-]*                  any character of: whitespace (\n, \r,
                             \t, \f, and " "), '&', '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    jill                     'jill'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
JavaScript:

const text=“jackjill | jack jill | jack&jill | jack&jill | jack-&-jill | jack and jill | jack and jill | jack n jill jack认识jill”;
console.log(text.replace(/\b(jack)[\s&-]*(?:(?:n |和)[\s&-]*)?(jill)\b/gi,$1和$2')使用

*解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    jack                     'jack'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [\s&-]*                  any character of: whitespace (\n, \r, \t,
                           \f, and " "), '&', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      n                        'n'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      and                      'and'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    [\s&-]*                  any character of: whitespace (\n, \r,
                             \t, \f, and " "), '&', '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    jill                     'jill'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
JavaScript:

const text=“jackjill | jack jill | jack&jill | jack&jill | jack-&-jill | jack and jill | jack and jill | jack n jill jack认识jill”;

console.log(text.replace(/\b(jack)[\s&-]*(?:(?:n |和)[\s&-]*)?(jill)\b/gi,$1和$2')试试这个
杰克。*?吉尔
试试这个
杰克。*?吉尔