Javascript 为什么可以';您是否在捕获的组上使用函数(是否有解决方法)?
在javascript中,如果我执行以下操作:Javascript 为什么可以';您是否在捕获的组上使用函数(是否有解决方法)?,javascript,Javascript,在javascript中,如果我执行以下操作: “start.content.end”。替换(/start。.*.end/,“$1.toUpperCase()” 我得到:content,而不是预期的content 但它适用于字符串文字,例如: “start.content.end”。替换(/start.(.*.end/,“$1.toUpperCase()+”_小写“.toUpperCase()) “内容”小写“ 为什么会出现这种情况,有什么办法可以解决吗?您的.toUpperCase和.toLo
“start.content.end”。替换(/start。.*.end/,“$1.toUpperCase()”
我得到:content
,而不是预期的content
但它适用于字符串文字,例如:
“start.content.end”。替换(/start.(.*.end/,“$1.toUpperCase()+”_小写“.toUpperCase())
“内容”小写“
为什么会出现这种情况,有什么办法可以解决吗?您的
.toUpperCase
和.toLowerCase
字符串将立即被解析,然后解释器将结果中的$#
替换为捕获组。这是:
.replace(/start.(.*).end/,"$1".toUpperCase())
只是
.replace(/start.(.*).end/,"$1")
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_LOWERCASE")
"start.content.end".replace(/start.(.*).end/,"$1_LOWERCASE")
而.replace
将传递$1
,并将其替换为第一个捕获组
同样地:
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
只是
.replace(/start.(.*).end/,"$1")
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_LOWERCASE")
"start.content.end".replace(/start.(.*).end/,"$1_LOWERCASE")
因此,“$1_LOWERCASE”
被传递到。replace
,它解析它,看到$1
,并用捕获组替换该$1
您的两个代码段基本上都在做相同的事情—解析一个普通字符串,然后将其传递给.replace
,它将替换其中的$\
如果您希望能够对捕获组执行特定操作-除了使用$#
s进行普通字符串插值外,还可以使用函数,例如:
console.log(
“开始。内容。结束”
.替换(
/开始。(.*)结束/,
(match,g1)=>g1.toUpperCase()
)
)
您的.toUpperCase
和.toLowerCase
字符串将立即被解析,然后解释器将结果中的$\code>替换为捕获组。这:
.replace(/start.(.*).end/,"$1".toUpperCase())
只是
.replace(/start.(.*).end/,"$1")
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_LOWERCASE")
"start.content.end".replace(/start.(.*).end/,"$1_LOWERCASE")
而.replace
将传递$1
,并将其替换为第一个捕获组
同样地:
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
只是
.replace(/start.(.*).end/,"$1")
"start.content.end".replace(/start.(.*).end/,"$1".toUpperCase() + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_lowercase".toUpperCase())
"start.content.end".replace(/start.(.*).end/,"$1" + "_LOWERCASE")
"start.content.end".replace(/start.(.*).end/,"$1_LOWERCASE")
因此,“$1_LOWERCASE”
被传递到。replace
,它解析它,看到$1
,并用捕获组替换该$1
您的两个代码段基本上都在做相同的事情—解析一个普通字符串,然后将其传递给.replace
,它将替换其中的$\
如果您希望能够对捕获组执行特定操作-除了使用$#
s进行普通字符串插值外,还可以使用函数,例如:
console.log(
“开始。内容。结束”
.替换(
/开始。(.*)结束/,
(match,g1)=>g1.toUpperCase()
)
)因为它就是这样工作的。.replace
必须接收一个函数引用,它可以使用指定的参数调用该函数引用
您的尝试将对文本字符串$1
本身调用一个方法,使其$1
(没有大写字符),然后将其传递给。replace
。replace
甚至不知道(也不能通过设计知道)。toUpperCase
同样的情况也发生在:
"$1".toUpperCase() + "_lowercase".toUpperCase()
…这与写作基本相同:
"$1_LOWERCASE"
…因为.toUpperCase
作用于替换字符串本身,而不是由.replace
要使由$1
表示的选择大写,必须将替换器回调传递给.replace
,该回调将其作为参数接收,并允许您连接到逻辑:
"start.content.end".replace(/start.(.*).end/, (match, firstCapturing) => firstCapturing.toUpperCase())
因为它就是这样工作的。.replace
必须接收一个函数引用,它可以使用指定的参数调用该函数引用
您的尝试将对文本字符串$1
本身调用一个方法,使其$1
(没有大写字符),然后将其传递给。replace
。replace
甚至不知道(也不能通过设计知道)。toUpperCase
同样的情况也发生在:
"$1".toUpperCase() + "_lowercase".toUpperCase()
…这与写作基本相同:
"$1_LOWERCASE"
…因为.toUpperCase
作用于替换字符串本身,而不是由.replace
要使由$1
表示的选择大写,必须将替换器回调传递给.replace
,该回调将其作为参数接收,并允许您连接到逻辑:
"start.content.end".replace(/start.(.*).end/, (match, firstCapturing) => firstCapturing.toUpperCase())