I am not able to understand how to check for brackets (opening and closing) in regex. Can anybody help me or provide me reference of regex resource that tells about the bracket clause?
Thanks in advance!
My code so far
function telephoneCheck(str) {
let newstr = str.replace(/ /g,"");
console.log(newstr[0])
let result = (/^([1])?(\d{3})(-?)(\d{3})(-?)(\d{4}$)/).test(newstr);
console.log(result, newstr, newstr[0]);
return result;
}
telephoneCheck("555-555-5555");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.
That doesn’t work because closing bracket should exist only in case of opening bracket. If we use ( or ) it passes test even when either of the brackets are missing.
yes, i suppose you already figured out, you need to use the or operator to let the regex match 2 different cases, one with brackets and one without, but i was simply answering directly to your question, how to refer to parentheses