遇到問題
input框中輸入全角空格,提交時,使用以下方法並沒有被過濾,表單提交成功。
const removeSpace = function(str) {
// 去除空格
if (!str) {
return ''
} else {
return str.replace(/\s/gi, '')
}
}
解決方法
const removeSpace = function(str) {
// 去除空格
if (!str) {
return ''
} else {
return str.replace(/(\s*)|(\s*$)/g, '')
}
}
擴展--去除字符串首尾的空格
const removeSpace = function(str) {
// 去除空格
if (!str) {
return ''
} else {
return str.replace(/(^\s*)|(\s*$)/g, '')
}
}