1. 程式人生 > >js校驗規則--去空格、加空格

js校驗規則--去空格、加空格

字符串 直觀 abcd acc pre str 號碼 replace nbsp

為了更加直觀,有些號碼需要加空格:

// 拼接空格,每4位加一個空格
let bankAccount = ‘6228888888888888888‘;
let blank_value = bankAccount.replace(/\s/g,‘‘).replace(/(.{4})/g,"$1 ").trim();
console.log(blank_value);

字符串去中間空格:

// 字符串去中間空格
let value = ‘ab cd efg‘;
let temp = value.replace(/[ ]/g, "");
console.log(temp);

字符串去首尾空格:

let str = ‘  abcdefg   ‘;
let new_str = str.trim();
console.log(new_str);

js校驗規則--去空格、加空格