使用javascript仅显示银行帐户中的最后4位数字

Scr*_*Guy 3 javascript

我需要帮助Javascript.我需要替换包含银行帐号的文本字段的最后4位之前的许多字符.我在网上搜索了这个,但找不到一个有效的代码.我确实在stackoverflow上找到了一个关于信用卡的代码,

new String('x', creditCard.Length - 4) + creditCard.Substring(creditCard.Length - 4);
Run Code Online (Sandbox Code Playgroud)

我刚用accounNumObject替换了creditCard:

var accounNumObject = document.getElementById("bankAcctNum")
Run Code Online (Sandbox Code Playgroud)

输入非常简单.

<cfinput type="text" name="bankAcctNum" id="bankAcctNum" maxlength="25" size="25" value="#value#" onblur="hideAccountNum();">
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

ale*_*lex 13

要替换x除JavaScript中最后四个字符之外的字符串,您可以使用(假设str保存字符串)...

var trailingCharsIntactCount = 4;

str = new Array(str.length - trailingCharsIntactCount + 1).join('x')
       + str.slice(-trailingCharsIntactCount);
Run Code Online (Sandbox Code Playgroud)

jsFiddle.

你也可以使用正则表达式......

str = str.replace(/.(?=.{4})/g, 'x');
Run Code Online (Sandbox Code Playgroud)

如果要添加4变量,请使用构造函数构造正则表达式RegExp.

jsFiddle.

如果你有幸获得支持,也......

const trailingCharsIntactCount = 4;

str = 'x'.repeat(str.length - trailingCharsIntactCount)
        + str.slice(-trailingCharsIntactCount);
Run Code Online (Sandbox Code Playgroud)

填充工具为String.prototype.repeat()可用的.


Col*_*een 5

这是一个小提示,显示您的要求:

http://jsfiddle.net/eGFqM/1/

<input id='account' value='abcdefghijklmnop'/>
<br/>
<input id='account_changed'/>
Run Code Online (Sandbox Code Playgroud)
var account = document.getElementById('account');
var changed = document.getElementById('account_changed');

changed.value = new Array(account.value.length-3).join('x') + 
    account.value.substr(account.value.length-4, 4);
Run Code Online (Sandbox Code Playgroud)

编辑:更新了小提琴以解决由alex指出的一个问题