简体   繁体   中英

How to match only if the both brackets are there in the string?

^[-+(]?\d+([,.]\d+)*([\)])*$

I am using the above regular expression for matching strings. one case i need to check

Testing string :

(111,1,1,1.3.3 should be failed

(111,1,1,1.3.3)  should be passed
+1.1.1.1,1       should be passed
-1111            should be passed
-2,2,2,2.4.4     should be passed
2,3              should be passed

The above string is matching the regex, but i want to check if the string contains first bracket it should check for end match with bracket.

try this regex:

 function validate(){ var regex = /^[-+(]?\\d+([,.]\\d+)*([\\)])+/gi; var value = document.getElementById("txtInput").value; console.log(value); document.getElementById("result").innerHTML = regex.test(value); } 
 <input type="text" id="txtInput" value="(111,1,1,1.3.3"> <p id="result"></p> <button onclick="validate()">Validate</button> 

You can use ored regex here.

^(?:[+-]?\(\d+([,.]\d+)*\)|[+-]?\d+([,.]\d+)*)$

See demo.

https://regex101.com/r/hE4jH0/19

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM