![](/img/trans.png)
[英]Regular expression in javascript to not allow only whitespaces in a field but allow string with whitespaces and also allow empty field
[英]Javascript Regular Expression - Filter only integer string also negative
我正在嘗試創建一個字符串的正則表達式驗證,我想檢查string是整數字符串,是正數還是負數。
我嘗試創建表達式,但只能過濾帶有符號“-”的數字,但是我無法將其以“-”開頭(可選)並在末尾包含任何數字的真實字符串進行匹配。
這是我的嘗試:
var intRegex = /^[-?\d]+$/g;
console.log(intRegex.test('55')); // true
console.log(intRegex.test('artgz')); // false
console.log(intRegex.test('55.3')); // false
console.log(intRegex.test('-55')); // true
console.log(intRegex.test('--55')); // true but I don't want this true
console.log(intRegex.test('5-5')); // true but I don't want this true
任何想法?
您可以使用/^-?\\d+$/
,您只希望連字符(-)0或1次,所以您使用? 在-之后,並且\\ d可以是1次或多次,因此只能將+用於\\ d。
var intRegex = /^[-]?\\d+$/g; console.log(intRegex.test('55')); // true console.log(intRegex.test('artgz')); // false console.log(intRegex.test('55.3')); // false console.log(intRegex.test('-55')); // true console.log(intRegex.test('--55')); // true but I don't want this true console.log(intRegex.test('5-5')); // true but I don't want this true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.