gpt4 book ai didi

Javascript 搜索和替换

转载 作者:数据小太阳 更新时间:2023-10-29 04:33:05 24 4
gpt4 key购买 nike

我想在 Javascript(伪代码)中执行以下操作:

myString.replace(/mypattern/g, f(currentMatch));

即替换字符串不是固定的,而是当前匹配的函数。

最佳答案

MDC声称你可以做到这一点:

function styleHyphenFormat(propertyName)
{
function upperToHyphenLower(match)
{
return '-' + match.toLowerCase();
}
return propertyName.replace(/[A-Z]/, upperToHyphenLower);
}

或者更笼统地说:

myString.replace(/mypattern/g, function(match){
return "Some function of match";
});

关于Javascript 搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/512411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com