[英]text length more than 3 string than replace rest with another string
How to replace the rest of a string more than 3 character swith another character? 如何替换超过3个字符的其余字符串替换另一个字符?
ex : 例如:
var str = "John Doe";
I want output with 我想要输出
Johxxxxx
Use String#replace
method with a callback function 将
String#replace
方法与回调函数一起使用
var str = "John Doe"; console.log( str.replace(/^(.{3})(.+)$/, function(_, m1, m2) { return m1 + // first 3 characters new Array(m2.length + 1).join('x'); // generate string with length of remaining character }) )
Or use String#slice
method. 或者使用
String#slice
方法。
var str = "John Doe"; console.log( str.slice(0, 3) + // get first three character new Array(str.length > 2 ? str.length - 2 : 0).join('x') // generate string with remaining length )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.