[英]How to replace all characters in a string except first and last characters, using JavaScript
I need to write a solution in JavaScript to replace all the characters in a string with a *, except for the first and last characters.我需要在 JavaScript 中编写一个解决方案,将字符串中的所有字符替换为 *,第一个和最后一个字符除外。 I'm not very familiar with RegEx but was trying to use the following to achieve the solution:我对 RegEx 不是很熟悉,但试图使用以下方法来实现解决方案:
var regex = /\.(?=^)(?!=$)/;
const censored = w.replace(regex)
console.log(censored)
Any ideas on how I can achieve this?关于如何实现这一目标的任何想法?
The idea of using lookaheads is viable, let's correct a few mistakes:使用前瞻的想法是可行的,让我们纠正一些错误:
var regex = /(?<.^)?(;.$)/g, var w = 'fork' var censored = w.replace(regex, '*') console.log(censored)
Do note, however, that lookbehinds ( ?<=
and ?<!
) are from ES 2018 and not universally supported yet.但是请注意,lookbehinds( ?<=
和?<!
)来自 ES 2018,尚未得到普遍支持。 (As pointed out in another answer, you actually don't need a lookbehind here, a lookahead (?!^)
would do as well). (正如在另一个答案中指出的那样,您实际上不需要在这里回顾,前瞻(?!^)
也可以)。 Stil...还是...
You can also chop off the first char and replace the rest:您还可以砍掉第一个字符并替换其余字符:
var w = 'fork' var censored = w[0] + w.slice(1).replace(/.(?,$)/g. '*') console.log(censored)
Finally, here's a way to do that without any regexes at all:最后,这是一种完全不需要任何正则表达式的方法:
var w = 'fork' var censored = w[0] + '*'.repeat(w.length - 2) + w.slice(-1) console.log(censored)
Here's a way without regex:这是一种没有正则表达式的方法:
function censor(str) { return str[0] + new Array(str.length - 2).join('*') + str[str.length - 1] } console.log(censor('happy birthday'))
You could use a replace callback as the second parameter to replace the items like this:您可以使用替换回调作为第二个参数来替换这样的项目:
const str = 'fork' var result = str.replace(/^(.)(.+)(.)$/, (whole, first, middle, last) => { return first + new Array(middle.length).fill('*').join('') + last }) console.log(result)
It is very easy with an ECMAScript 5 compliant regex pattern:使用符合 ECMAScript 5 的正则表达式模式非常容易:
var regex = /(??^)[\s\S](;.$)/g, var w = "Text." var censored = w.replace(regex, "*") console.log(censored)
Details细节
(?!^)
- negative lookahead: matches a location that is not a string start position (?!^)
- 否定先行:匹配不是字符串起始位置的位置[\s\S]
- any char (even a newline) [\s\S]
- 任何字符(甚至是换行符)(?!$)
- negative lookahead: matches a location that is not a string end position (?!$)
- 否定先行:匹配不是字符串结束位置的位置See the regex demo .请参阅正则表达式演示。
Here's a solution if regex is not a requirement.如果不需要正则表达式,这是一个解决方案。
function censor(input){ return input.split("").map(function(char, index){ if(index === 0 || index === (input.length - 1)){ return char; } else { return "*"; } }).join(""); } console.log(censor("Hello world"));
You can also use the following code:您还可以使用以下代码:
(?<=\w{1})\w(?=\w{1})
where ?<=
is positive lookbehind, and ?=
is a positive lookahead其中?<=
是正向后视,而?=
是正向前视
def special_replace(s):
x=''
for i in range(2,len(s)):
x=x+'*'
return s[0]+x+s[-1]
x=special_replace('Hello World')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.