在字符串之间替换字符

时间:2023-01-07 11:46:31

Suppose I have an email address, 'abcdef@gmail.com'. I want to replace all the characters between 'a' and 'f' so the result would look like 'a****f@gmail.com'.

假设我有一个电子邮件地址“abcdef@gmail.com”。我想要替换“a”和“f”之间的所有字符,这样结果看起来就像“一个****f@gmail.com”。

Trying to do this with a regex and replace

尝试使用regex并替换

str.replace(/^(.*?)@/gi, '*');

But the results look like this

但结果是这样的。

*gmail.com

Is there a way to do what I need?

有办法做我需要的吗?

2 个解决方案

#1


1  

I'd use a replace with a callback, where the user middle part can be also replaced with *s:

我将使用回调替换,用户中间部分也可以用*s替换:

var email = "abcdef@gmail.com";
document.write(email.replace(/^(.)(.*)(.@[^@]*)$/, function(m, g1, g2, g3) {
  return g1 +  g2.replace(/./g, "*") + g3;
}));

Here is how the "outer" /^(.)(.*)(.@[^@]*)$/ regex works:

这是“外”/ ^(.)(. *)(.@ ^ @ *)美元/正则表达式是:

  • ^ - matches start of a string
  • ^ -匹配字符串的开始
  • (.) - Group 1: any first character
  • ()-第一组:任何第一个角色。
  • (.*) - Group 2: any characters up to the character before the last @`
  • (.*) -第2组:在最后一个@ '前的任何字符
  • (.@[^@]*) - Group 3: one character before the last @, then @ and then any 0+ characters other than @ up to...
  • (.@[^ @]*)- 3组:一个字符之前最后一个@,@然后以外的任何字符0 + @了……
  • $ - end of string
  • $末端的字符串。

The .replace(/./g, "*") will just replace any character with *. And it will be done only on the Group 2.

.replace(/。/g, "*")将用*替换任何字符。它只在第二组进行。

The regex you suggested in the comment should also work.

您在评论中建议的regex也应该有效。

/(?!^).(?=[^@]+@)/g matches any character but a newline that is not the first character ((?!^)) and that has 1+ characters other than @ after it and a @.

/(? ! ^)(? =(^ @)+ @)/ g匹配任何但换行,不是第一个字符((? ! ^)),有1 +字符后除了@ @。

var re = /(?!^).(?=[^@]+@)/g;
document.body.innerHTML = "fake@gmail.com".replace(re, "*");

#2


2  

This is not an answer to your actual question, but I'd like to challenge you that your idea is not a good one. It's best not to show how long an email address is by replacing the internal letters with the same number of *s. It's better to use a fixed number of *s.

这不是你实际问题的答案,但我想向你挑战,你的想法并不好。最好不要用相同数量的*来代替内部信件来显示电子邮件地址的长度。最好使用固定数量的*s。

You seem to be using javascript, which doesn't have lookbehind assertions, and capturing in this case may be simpler to understand too, so I'd do this to replace with a constant number of *s

您似乎使用的是javascript,它没有lookbehind断言,在这种情况下,捕获也可能更容易理解,所以我用常量*s替换它

str.replace(/^(.).*(.@)/, '$1***$2')

#1


1  

I'd use a replace with a callback, where the user middle part can be also replaced with *s:

我将使用回调替换,用户中间部分也可以用*s替换:

var email = "abcdef@gmail.com";
document.write(email.replace(/^(.)(.*)(.@[^@]*)$/, function(m, g1, g2, g3) {
  return g1 +  g2.replace(/./g, "*") + g3;
}));

Here is how the "outer" /^(.)(.*)(.@[^@]*)$/ regex works:

这是“外”/ ^(.)(. *)(.@ ^ @ *)美元/正则表达式是:

  • ^ - matches start of a string
  • ^ -匹配字符串的开始
  • (.) - Group 1: any first character
  • ()-第一组:任何第一个角色。
  • (.*) - Group 2: any characters up to the character before the last @`
  • (.*) -第2组:在最后一个@ '前的任何字符
  • (.@[^@]*) - Group 3: one character before the last @, then @ and then any 0+ characters other than @ up to...
  • (.@[^ @]*)- 3组:一个字符之前最后一个@,@然后以外的任何字符0 + @了……
  • $ - end of string
  • $末端的字符串。

The .replace(/./g, "*") will just replace any character with *. And it will be done only on the Group 2.

.replace(/。/g, "*")将用*替换任何字符。它只在第二组进行。

The regex you suggested in the comment should also work.

您在评论中建议的regex也应该有效。

/(?!^).(?=[^@]+@)/g matches any character but a newline that is not the first character ((?!^)) and that has 1+ characters other than @ after it and a @.

/(? ! ^)(? =(^ @)+ @)/ g匹配任何但换行,不是第一个字符((? ! ^)),有1 +字符后除了@ @。

var re = /(?!^).(?=[^@]+@)/g;
document.body.innerHTML = "fake@gmail.com".replace(re, "*");

#2


2  

This is not an answer to your actual question, but I'd like to challenge you that your idea is not a good one. It's best not to show how long an email address is by replacing the internal letters with the same number of *s. It's better to use a fixed number of *s.

这不是你实际问题的答案,但我想向你挑战,你的想法并不好。最好不要用相同数量的*来代替内部信件来显示电子邮件地址的长度。最好使用固定数量的*s。

You seem to be using javascript, which doesn't have lookbehind assertions, and capturing in this case may be simpler to understand too, so I'd do this to replace with a constant number of *s

您似乎使用的是javascript,它没有lookbehind断言,在这种情况下,捕获也可能更容易理解,所以我用常量*s替换它

str.replace(/^(.).*(.@)/, '$1***$2')