-1
var reg=/[\(\d{3}\)]/g;
(reg).test(str); // 

the above code is true for any number of parenthesis str="(((555)))", str="((555))". I want true only when there is only one pair of parenthesis str="(555)" and for any other string it should be false;

Md Sifatul Islam
  • 846
  • 10
  • 28

1 Answers1

0

It looks like you are unclear on the character class operator. For what you have asked, you want to anchor at the beginning (^) and end ($) of the string, and you want to find a parenthesis (\() followed by 3 digits (\d{3} or \d\d\d) followed by a final parenthesis (\)). Altogether:

var reg = /^\(\d{3}\)$/;

You were close, just needed to swap the brackets with the anchors.

hunteke
  • 3,648
  • 1
  • 7
  • 17