非常糟糕的正则表达式,所以在这里寻求帮助我写的函数是接受一个输入,并返回规范化的输出,所以输入必须匹配,而不是列出所有可能的组合,有什么方法可以使用RegExp吗?
例如jackjill | jack-jill | jack - jill | jack&jill | jack-&-jill | jack- and -jill | jackandjill | jack -and- jill | jack n jill |...,函数将返回'Jack & Jill‘
因此,input.match(/jack -&n*jill/)涵盖了空格-,n,但是‘input.match’又如何呢?
发布于 2020-09-11 16:39:21
您可以使用可选的字符类扩展模式,并使用|扩展alternation
\bjack(?:[ &-]+|[ -]?(?:and|n)[ -]?)?jill\b如果n或and之前和之后的内容应该是相同的,您可以对前面的part使用捕获组,并使用反向引用来匹配它之后的相同内容。
\bjack(?:[ &-]+|([ -]?)(?:and|n)\1)?jill\b说明
\bjack单词边界,然后匹配jack(?:非捕获组[ &-]+匹配空格、&或1(?:and|n)可选地匹配捕获组中的空格或反向引用与组1中捕获的内容匹配and或-
关闭组并使ik optional
jill\b与jill匹配,后跟单词边界发布于 2020-09-11 20:32:50
发布于 2020-09-12 04:11:23
使用
replace(/\b(jack)[\s&-]*(?:(?:n|and)[\s&-]*)?(jill)\b/gi, '$1 & $2')参见proof。
*解释
--------------------------------------------------------------------------------
\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 charJavaScript:
const text = " jackjill | jack jill | jack-jill | jack&jill | jack & jill | jack-&-jill | jack and jill | jackandjill | jack-and-jill | jack n jill jack knows Jill";
console.log(text.replace(/\b(jack)[\s&-]*(?:(?:n|and)[\s&-]*)?(jill)\b/gi, '$1 & $2'));
https://stackoverflow.com/questions/63843547
复制相似问题