简体   繁体   English

创建正则表达式以替换具有相同字符的字符串的每个匹配字符

[英]Creating a regex to replace each matched character of a string with same character

In my application, I have an alphanumeric string being passed into my function. 在我的应用程序中,我将一个字母数字字符串传递给我的函数。 This string is typically 17 characters, but not always. 此字符串通常为17个字符,但并非总是如此。 I'm trying to write a regex that matches all but the last 4 characters in the string, and replaces them with X (to mask it). 我正在尝试编写一个匹配除字符串中最后4个字符之外的所有字符的正则表达式,并用X替换它们(以掩盖它)。

For example 例如

Input: HGHG8686HGHG8686H

Output: XXXXXXXXXXXXX686H

The Regex I wrote to perform the replace on the string is as follows 我写的正则表达式对字符串执行替换如下

[a-zA-Z0-9].{12}

Code: 码:

const maskedString = string.replace(/[a-zA-Z0-9].{12}/g, 'X');

The issue I'm having is that it's replacing all but the last 4 characters in the string with just that single X. It doesn't know to do that for every matched character. 我遇到的问题是它只用单个X替换除字符串中的最后4个字符以外的所有字符。它不知道为每个匹配的字符执行此操作。 Any ideas? 有任何想法吗?

you can use a function inside replace to do this, something like this will do: 你可以在replace中使用一个函数来做到这一点,这样的事情会做:

 var str = "HGHG8686HGHG8686H" var regexp = /[a-zA-Z0-9]+(?=....)/g; var modifiedStr = str.replace(regexp, function ($2) { return ('X'.repeat($2.length +1)); }); console.log(modifiedStr); 

Look ahead ( ?= ) to make sure there are at least four following characters. 向前看( ?=以确保至少有四个以下字符。

 const regex = /.(?=....)/g; // ^ MATCH ANYTHING // ^^^^^^^^ THAT IS FOLLOWED BY FOUR CHARS function fix(str) { return str.replace(regex, 'X'); } const test = "HGHG8686HGHG8686H"; // CODE BELOW IS MERELY FOR DEMO PURPOSES const input = document.getElementById("input"); const output = document.getElementById("output"); function populate() { output.textContent = fix(input.value); } input.addEventListener("input", populate); input.value = test; populate(); 
 <p><label>Input: </label><input id="input"></p> <p>Output: <span id="output"></span></p> 

A non-regexp solution: 非正则表达式解决方案:

 const test = "HGHG8686HGHG8686H"; function fix(str) { return 'X'.repeat(str.length - 4) + str.slice(-4); } console.log(fix(test)); 

You will not find String#repeat in IE. 你不会在IE中找到String#repeat

The simple version: (Easier to read) 简单版本:(更易于阅读)

const maskedString = string.replace(/(.{4})$|(^(..)|(.))/g, 'X\1'); // or X$1

Now using: [a-zA-Z0-9] 现在使用:[a-zA-Z0-9]

const maskedString = string.replace(/([a-zA-Z0-9]{4})$|(^([a-zA-Z0-9]{2})|([a-zA-Z0-9]{1}))/g, 'X\1'); // or X$1

Note: The reason i match on the START PLUS TWO characters is to offset the first match. 注意:我在START PLUS TWO字符上匹配的原因是抵消第一场比赛。 (The final 4 characters that are appended at the end.) (最后附加的最后4个字符。)

You can achieve using following method: 您可以使用以下方法实现:

  var str = "HGHG8686HGHG8686H" var replaced='' var match = str.match(/.+/) for(i=0;i<match[0].length-4;i++){ str = match[0][i] replaced += "X" } replaced += match[0].substr(match[0].length-4) console.log(replaced); 

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

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