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

时间:2015-07-22 13:53:08

标签: javascript regex replace

如何修改:

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”;

大写的工作,但忽略修剪功能

1 个答案:

答案 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"重写它。紧挨着它。