简体   繁体   English

文本长度超过3个字符串,而不是用另一个字符串替换rest

[英]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.

相关问题 如果文本字符串长度超过3个字符,则隐藏div - Hide div if text string length is more than 3 characters 如果文本字符串长度在php while循环中超过3个字符,则隐藏父div - Hide parent div if text string length is more than 3 characters in a php while loop jquery .replace()-如何替换同一行中的多个字符串? - jquery .replace() - How to replace more than one string in the same line? 仅当包含的字符串长度大于X时才替换 - Replace only if the containing string length is greater than X 如何用javascript中的“”(空格)替换字符串中长度小于3的所有单词? - How to replace all words of length less than 3 in a string with “ ” (space) in javascript? 如果匹配的字符串长度大于指定的限制,则用Javascript正则表达式替换 - Javascript regex replace if the matched string length is greater than limit specified 将所有字母和javascript字符串中的2个以上空格替换为空字符 - Replace All letters and more than 2 spaces in javascript string for empty character 如何同时替换 JavaScript 字符串中的多个单词? - How to replace more than one word in JavaScript string at a same time? 字符串比较比字符串长度快吗? - Is string comparison faster than string length? 数组到一个以上的字符串 - Array to more than one string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM