gpt4 book ai didi

javascript - 如何修改字符串内部字符,Javascript正则表达式

转载 作者:行者123 更新时间:2023-12-03 09:29:22 25 4
gpt4 key购买 nike

修改方法:

var a = ' z this is ok z ';
a = a.replace(/z(.*)z/, function(match){ return match.trim().toUpperCase();});
console.log(a); // output: " Z THIS IS OK Z "

我希望“ZTHIS IS OKZ”;

大写字母起作用,但 trim 功能被忽略

最佳答案

您正在将空格与(*) 匹配。更改为:

var a = ' z this is ok z ';

// Here, you'll notice that I added the spaces next to the "z" character.
a = a.replace(/z (.*?) z/, " Z$1Z ").toUpperCase();

console.log(a); // output: " ZTHIS IS OKZ "

它的作用是匹配“z”之间的所有内容,然后用紧邻的“Z”重写它。

关于javascript - 如何修改字符串内部字符,Javascript正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31565190/

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