问题描述
我有一个有趣的挑战我在这里找不到答案。我有一串文字,可能可能包含一个帐号。
例如:
I have a fun challenge I couldn't find an answer for here. I have a string of text, that could potentially contain an account number.Example:
"Hi, my account number is 1234 5678 9012 2345 and I'm great."
帐号可以有多种形式,因为它是由用户输入的:
The account number can come in many flavours, as it's entered by the user:
以下基本和潜在可能性:
Basic and potential possibilities below:
1234 1234 1234 1234
1234 1234 1234 1234 1
BE12 1234 1234 1234
1234-1234-1234-1234
1234.1234.1234.1234
1234123412341234
12341234 1234 1234
1234-1234-1234-1234-1
1234.1234.1234.1234.1
12341234123412341
12341 234 1234 12341
BE12-1234-1234-1234
be12-1234-1234 1234
Be12.1234.1234-1234
BE12123412341234
(基本上是带有连字符,空格或中间点的整数,但IBAN格式除外,开头有两个字符)
(basically integers with hyphen, space or a dot in the middle, with the exception of IBAN format, that has two characters at the beginning)
我需要什么输出是屏蔽的全部,除了最后四位数字。
What I need as output is everything masked, except the last four digits.
"Hi, my account number is **** **** **** 2345 and I'm great."
我认为我应该如何解决这个问题:
How I think I should approach this problem:
- 分析每个字符串并尝试查找上述帐号。模式
- 创建一个替换帐户号的神奇正则表达式。他们我需要的方式
- 如果有帐号,请使用此RegEx来执行此操作。
你的方法是什么?
谢谢!
推荐答案
你可以匹配上述所有内容:
You could match all of the above with:
\b[\dX][-. \dX]+(\d{4})\b
...并更换它与 *
x strlen(匹配) - 4
+ \ 1
,请参阅。
JavaScript
:... and replace it with *
x strlen(match) - 4
+ \1
, see a demo on regex101.com.
In
JavaScript
:var string = "Hi, my account number is 1234 5678 9012 2345 and I'm great.";
var new_string = string.replace(/\b[\dX][-. \dX]+(\d{4})\b/g, function(match, capture) {
return Array(match.length-4).join("*") + capture;
});
print(new_string);
参见。
这篇关于Javascript - 屏蔽字符串中的帐号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!