I want to verify whether the beginning or end of the string is a space. Why is the result true regardless of orgName?
var orgName = ""; var reg = /(^\s*|\s*$)/; if (reg.test(orgName)) { } else { alert("输入内容不符合规范"); }
"*" means 0 or more times, You should use "+" here to match at least once
Try:
const regx = /(^\s|\s$)/; console.log(regx.test('')); //false console.log(regx.test('aaa')); //false console.log(regx.test(' ')); //true console.log(regx.test(' aaa')); //true console.log(regx.test('aaa ')); //true console.log(regx.test(' aaa ')); //true
var orgName = " 112";var reg = /^[s ]|[ ]$/gi;if (reg.test(orgName)) {
alert("符合要求")
} else {
alert("输入内容不符合规范");
}
"*" means 0 or more times,
You should use "+" here to match at least once
Try:
var orgName = " 112";
var reg = /^[s ]|[ ]$/gi;
if (reg.test(orgName)) {
} else {
}