node.js 正则表达式字符串参数中的匹配组不起作用
Matching group in node.js regex string parameter not working
String.prototype.replace()
method documentation 说:
Specifying a string as a parameter
The replacement string can include the following special replacement patterns:
$$
: Inserts a "$".
$&
: Inserts the matched substring.
$`
: Inserts the portion of the string that precedes the matched substring.
$'
: Inserts the portion of the string that follows the matched substring.
$n
: Where n
is a positive integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp
object. Note that this is 1-indexed.
浏览器兼容性部分还指出 Node.js 支持 String.replace
命令。
但是,我在Node.js中尝试了以下方法:
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
我希望响应分别为 la
、la
和 ffffff
。什么在这里不起作用?
它不起作用,因为您的正则表达式与您的字符串不匹配(这可能是错误的)。
您在最后一个 l
之后缺少一个 a
,或者您必须删除最后一个 l
lalalalalalalffffff
=> lalalalalalalaffffff
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
如您的示例所示,您的示例输入是 lalalalalalalffffff
,但请注意第一个 f
之前的 l
。问题是你的正则表达式什么都不匹配,因为它找不到一个或多个 la
后跟一个 f
,因为那个愚蠢的 l
.
您可以在 Regex101.com
上亲自查看
如果您确实想保留额外的 l
,并让 </code> 给出 <code>la
的输出,您可以尝试:
(la)(la[l]?)*(f.*)
而且,如果你想让</code>匹配<code>lalalalalal
,你可以使用:
(la)((?:la[l]?)*)(f.*)
这也将匹配 lalala
for lalalalafffffff
String.prototype.replace()
method documentation 说:
Specifying a string as a parameter
The replacement string can include the following special replacement patterns:
$$
: Inserts a "$".$&
: Inserts the matched substring.$`
: Inserts the portion of the string that precedes the matched substring.$'
: Inserts the portion of the string that follows the matched substring.$n
: Wheren
is a positive integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was aRegExp
object. Note that this is 1-indexed.
浏览器兼容性部分还指出 Node.js 支持 String.replace
命令。
但是,我在Node.js中尝试了以下方法:
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
> 'lalalalalalalffffff'.replace(/(la)(la)*(f.*)/, "")
'lalalalalalalffffff'
我希望响应分别为 la
、la
和 ffffff
。什么在这里不起作用?
它不起作用,因为您的正则表达式与您的字符串不匹配(这可能是错误的)。
您在最后一个 l
之后缺少一个 a
,或者您必须删除最后一个 l
lalalalalalalffffff
=> lalalalalalalaffffff
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
console.log('lalalalalalalaffffff'.replace(/(la)(la)*(f.*)/, ""));
如您的示例所示,您的示例输入是 lalalalalalalffffff
,但请注意第一个 f
之前的 l
。问题是你的正则表达式什么都不匹配,因为它找不到一个或多个 la
后跟一个 f
,因为那个愚蠢的 l
.
您可以在 Regex101.com
上亲自查看如果您确实想保留额外的 l
,并让 </code> 给出 <code>la
的输出,您可以尝试:
(la)(la[l]?)*(f.*)
而且,如果你想让</code>匹配<code>lalalalalal
,你可以使用:
(la)((?:la[l]?)*)(f.*)
这也将匹配 lalala
for lalalalafffffff