大小寫轉換
1、轉小寫方法
var value = 'ABC'
value.toLowerCase()
console.log(value) // abc
2、轉大寫方法
var value = 'abc'
value.toUpperCase()
console.log(value) // ABC
全角轉半角
fullToHalf(val) {
let retVal = ''
for (let i = 0; i < val.length; i++) {
if (val.charCodeAt(i) == 12288) {
retVal += String.fromCharCode(val.charCodeAt(i) - 12256);
continue;
}
if (val.charCodeAt(i) > 65280 && val.charCodeAt(i) < 65375) {
retVal += String.fromCharCode(val.charCodeAt(i) - 65248);
} else {
retVal += String.fromCharCode(val.charCodeAt(i));
}
}
return retVal
}
清除空格
1、清除左側空格
var tmp= ' 123456'
tmp = tmp.replace(/(^\s*)/g, "")
2、清除右側空格
var tmp= '1234566 '
tmp = tmp.replace(/(\s*$)/g, "")
3、清除左右兩側空格
var tmp= ' 1234566 '
tmp = tmp.replace(/(^\s*)|(\s*$)/g, "")
4、清除所有空格
var tmp= ' 1234 566 '
tmp = tmp.replace(/\s/g, "")
僅數字
var tmp= '8888ABC'
tmp = tmp.replace(/\s/g, "")
僅字母
var tmp= 'ASJDKASD99'
tmp = tmp.replace(/[^a-zA-Z]/g, '')
僅字母和數字
var tmp= 'ASJDKASD99'
tmp = tmp.replace(/[\W]/g, '')