我该如何在这里以不同的方式分割数字,例如 XXX-XXX-XXXX?
String.prototype.toCardFormat = function () {
return this.replace(/[^0-9]/g, "").substr(0, 16).split("").reduce(cardFormat, "");
function cardFormat(str, l, i) {
return str + ((!i || (i % 4)) ? "" : "-") + l;
}
};
最佳答案
这里是更新的 fiddle 的链接。向您的函数添加 OR 条件。
function cardFormat(str, l, i) {
return str + ((!i || (i % 3) || i > 8) ? "" : "-") + l;
}
关于javascript - 输入掩码组的分割长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213609/