javascript - Regular validation of empty string
漂亮男人
漂亮男人 2017-06-12 09:32:21
0
3
848

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("输入内容不符合规范");
}
漂亮男人
漂亮男人

reply all(3)
某草草

"*" 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("输入内容不符合规范");

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template