isEmpty(obj){
var regu = "^[ ]+$"
var re = new RegExp(regu)
if(typeof obj == "undefined" || obj == null || obj == "" || re.test(obj)){
return true
}else{
return false
}
},
用法:
if(this.isEmpty(this.keyword)){
console.log('空字符')
}
var str = "任意 字符串"if (str.indexOf(" ") == -1) {
alert("没有空格")
} else {
alert("有空格")
}
用match和正则表达式来检测:<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
var str = "hello world 123"
var mm = /[a-z]+/
document.write(str.match(mm))
</script>
</body>
</html>