我一直在尝试制作 16 位有符号整数的正则表达式(-32768
到 32767
)引用这个 link of 32 bit integer .
到目前为止,我试过这个:
^-?([0-9]{1,5}|32[0-6]{3}|32[0-6]{2}|327[0-5][0-8])$|^(-32768)$
但是,它并不匹配。
请指导。
最佳答案
尝试(但是使用正则表达式不是个好主意 - if
更好)
let r=/^(-?(\d{1,4}|[012]\d{4}|3[01]\d{3}|32[0123456]\d{2}|327[012345]\d{1}|3276[01234567])|-32768)$/;
// test
console.log("-32768", r.test("-32768")) ;
console.log(" -9876", r.test("-9876")) ;
console.log(" 9876", r.test("9876")) ;
console.log(" 32767", r.test("32767")) ;
console.log("-32769", r.test("-32769")) ;
console.log(" 32768", r.test("32768")) ;
关于javascript - 16 位有符号整数的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56685973/