简体   繁体   English

替换 JavaScript 中的中间 n 个字符

[英]Replacing middle n characters in JavaScript

I have a phone number and I want to display it like censored form.我有一个电话号码,我想像审查形式一样显示它。 For example I have: +923451234567例如我有: +923451234567

I want to display it like: +923*******67我想显示为: +923*******67

Str.replace function will be used, but how:将使用Str.replace函数,但如何:

var str = '+923451234567'
str.replace(i_Dont_Know_What, '*');

So in this case string contains 13 characters.所以在这种情况下,字符串包含 13 个字符。 And I want to replace 5th-11th character with " * "我想用“*”替换第 5-11 个字符

I have found this somewhere, but this is not what I want.我在某处找到了这个,但这不是我想要的。

var str = "name(replace these parenthesis by @)domain.com";
var patt1 = /\(.*@\)/i;
document.write(str.replace(patt1,"@"));

How would I achieve this?我将如何实现这一目标?

Nice and simple one line replace with a substring match, no need for a messy regex here.漂亮而简单的一行替换为子字符串匹配,这里不需要凌乱的正则表达式。

 var str = '+923451234567'; console.log(str.replace(str.substring(4,11), "*******"));

有多种方法,一种方法是简单的 reg 表达式

 console.log("+923451234567".replace(/(\\+\\d{3})\\d{7}/,"$1*******"))

regex to keep 3 num characters at the beginning and 2 at the end正则表达式在开头保留 3 个 num 字符,在结尾保留 2 个字符

regex explanation: https://regex101.com/r/xR6pRD/1正则表达式解释: https : //regex101.com/r/xR6pRD/1

 var str = '+923451234567' str = str.replace(/(\\+?\\d{3})(\\d+)(\\d{2})/g, function(match, start, middle, end) { return start + "*".repeat(middle.length) + end; }); document.write(str);

You could use a dynamic approach by giving the length and take an offset for shifting the replacement charcters.您可以通过给出长度并采用偏移量来移动替换字符来使用动态方法。

 function replaceMiddle(string, n) { var rest = string.length - n; return string.slice(0, Math.ceil(rest / 2) + 1) + '*'.repeat(n) + string.slice(-Math.floor(rest / 2) + 1); }; console.log(replaceMiddle('+923451234567', 7)); console.log(replaceMiddle('+923451234567', 6)); console.log(replaceMiddle('+923451234567', 5)); console.log(replaceMiddle('+923451234567', 4)); console.log(replaceMiddle('+923451234567', 3)); console.log(replaceMiddle('+923451234567', 2)); console.log(replaceMiddle('+923451234567', 1));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

First we get the substring to be replaced:首先我们得到要替换的子串:

//you can use variables here too
var substringToBeReplaced = num.substring(4, 11); //4512345

then we replace the substring in the string:然后我们替换字符串中的子字符串:

var str = '+923451234567'
str.replace(substringToBeReplaced, '*******'); //result will be +923*******67

you can use another range instead of 4 - 11 if you want other set of numbers to be replaced.如果您希望替换其他一组数字,您可以使用另一个范围而不是4 - 11

 var str = '+923451234567'; var lol = str.replace(/^(\\+?[\\d]{3})\\d+(\\d{3})$/g,"$1*****$2"); console.log(lol);

I would just reconstruct a new string like so我会像这样重建一个新的字符串

replaceWithStar(index1,index2, str) {
    return str.substr(0, index1) + '*******'+ str.substr(index2, str.length);
}

try it is a complete method.试试是一个完整的方法。

 setStar(val) {
        let value = val.split('');
        let centerVal = value.slice(3,7);
        return value.slice(0,3).join("") + centerVal.map(()=> "*").join("") + value.slice(7,11).join("");
      }

i hope useful.我希望有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM