it’s only because it is in the .value property that it is a string. (it will always be a string no matter what you type)
regexes are hard. I spent maybe 2 hours plus getting mine to work.
Have you considered a simpler route like an array of regexes so you can match different variations?
Okay, thank you. I somehow didn’t know this until now.
thank you, i’ll try to do that. although i’m unsure how to make my if statement still function with these changes
your if statement had a logical error. I was trying to tell you before (in one of my earlier replies) but we got side-tracked with the whole toString discussion.
logical error? but i’m just checking if userInput
i s true or false and then saying what to do if true and if false…
show me your new if statement again. The last one was broken but there’s no point going over it if you’ve changed it.
Well, i’ve changed much but here it is anyway:
checkBtn.addEventListener("click", () => { if (userInput.value === "") { alert("Please provide a phone number") } else if (regex1.test(userInput.value) === true || regex2.test(userInput.value) === true) { results.innerText = `Valid US number: ${userInput.value}`; } else { results.innerText = `Invalid US number: ${userInput.value}`; } });
this looks decent now.
A shorter way to write (regex.test(value) == true) is simply
(regex.test(value)) as it is a truthy being returned.
changed to what you just said. But I still can’t have parenthesis around area code for some reason. Here’s what i tried: [(][\d][)], [([\d])], (\d), [(\d)]
tell me what you’re hoping this regex will match so I can try it on regex101
1 (555) 555-5555,
1(555)555-5555,
(555)555-5555
regex101 sees a syntax error in your pattern.
There’s an unmatched paren somewhere
yeah, i get that as well
it highlights the extra parenthesis in red
ps. if you wanna chat live, it may be better to go to fCC’s discord server
yes, whenever i try [([\d])]
, i always get one paren red even thou there’s nothing wrong with it
hold on, this is not your pattern this is your -array- of patterns?
i created two patterns : one without paren and one with. I hope that answers your question
I was wondering if this code was an array or a pattern?
I cannot tell… Usually patterns start and end with a slash.