如何修改:
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是OKZ”;
大写的工作,但忽略修剪功能
答案 0 :(得分:1)
您将空格与(*)
匹配。改为:
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"重写它。紧挨着它。