Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if string contains brackets with integer in it

I want to test if a string in javascript contains normal brackets with an integer in it

var str="(12) this is a test";
var pat=/(([\d]+))/;
if (pat.test(str)) 
    alert("true");

Works fine, but it also returns true when the string looks like this

var str="12) test";
var str="(12 test";

which should return false.

like image 697
Chris Avatar asked Oct 27 '25 09:10

Chris


1 Answers

Escape brackets with \:

/\([\d]+\)/.test(str);
like image 66
dimusic Avatar answered Oct 29 '25 22:10

dimusic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!