Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Is there a way I can test a string using regex to be true only if ( & ) both happen at least once? The regex has to make sure both are used

In this case it's returning true but I need it to return false. I tried adding [()]{0,1} but I think it's only referring to just one"(or)" is there a way I can do [(&)]{0,1} so that "(" and ")" both have to happen at least once. Thank you

function telephoneCheck(str) {


const regex = /^[1]{0,1}[-\s]?[(]?\d{3}[-)\s]?[\s]?[\d]{3}[-\s]?[\d]{4}$/g
return regex.test(str)
}

console.log(telephoneCheck("1 555)555-5555"));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Given that your example involves telephone numbers, I'm going to assume that you are just looking for one pair of parentheses, and in one particular spot. You can use alternation to check for this:

/^1?[-\s]?(?:\(\d{3}\)|\d{3})[-\s]?\d{3}[-\s]?\d{4}$/g

The key is the non-capturing group starting with (?: which will pick either a sequence of three digits surrounded by parentheses or a sequence of three digits without parentheses. I also simplified the rest of your regex as you don't need to use so many brackets.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!