input type="text" v-model="bankCard" @keyup="bankCardKeyup">
bankCardKeyup (e) {
let self = this
// 如果是刪除鍵,則不加空格,且刪去字符串末端空格
if (e.keyCode === 8) {
self.bankCard = self.bankCard.replace(/[\s\n\t]+$/g, "")
return
}
self.bankCard = self.bankCard.replace(/[^0-9|' ']*/g, '')
let value = self.bankCard.replace(/[^0-9]*/g, '')
if (value.length > 4 value.length = 8) {
self.bankCard = value.slice(0, 4) + ' ' + value.slice(4, value.length)
}
else if (value.length > 8 value.length = 12) {
self.bankCard = value.slice(0, 4) + ' ' + value.slice(4, 8) + ' ' + value.slice(8, value.length)
}
else if (value.length > 12 value.length = 16) {
self.bankCard = value.slice(0, 4) + ' ' + value.slice(4, 8) + ' ' + value.slice(8, 12) + ' ' + value.slice(12, value.length)
if (value.length === 16) {
// 調(diào)用識(shí)別銀行卡函數(shù)
}
}
else if (value.length > 16) {
self.bankCard = value.slice(0, 4) + ' ' + value.slice(4, 8) + ' ' + value.slice(8, 12)+ ' ' + value.slice(12, 16) + ' ' + value.slice(16, value.length)
if (value.length === 19) {
// 調(diào)用識(shí)別銀行卡函數(shù)
}
else if (value.length > 19) {
self.bankCard = value.slice(0, 4) + ' ' + value.slice(4, 8) + ' ' + value.slice(8, 12)+ ' ' + value.slice(12, 16) + ' ' + value.slice(16, 19)
}
}
},
input type="text" v-model="bankCard" @keyup="bankCardKeyup">
bankCardKeyup () {
let self = this
self.bankCard = self.bankCard.replace(/\s/g,'').replace(/[^\d]/g,'').replace(/(\d{4})(?=\d)/g,'$1 ')
}
以上所述是小編給大家介紹的正則表達(dá)式實(shí)現(xiàn)字符串每4位后自動(dòng)加空格效果(兩種方法),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!