Three minor additions: you can omit the ternary in your case in favor of a logical gate and you should use /regex/.test(string) instead of string.match(/regex/) if you don't want to do anything with the result and lastly, you don't want domains like localhost.mydomain.com to be interpreted as secure, so better make sure that regex captures the whole domain string - so now it looks like this:
Just a coder and a dad. I love my family and I love to code!!!! started coding at 11, so I have 25 years under my belt. Still love learning about it every day. Black lives matter!
Aw! you're so right! I should have used test vs match, also I should have added in $ for an exact match. I'll update this! Part of the fun was to show people they can resort to regex when doing multiple test against a single variable. If it was another scenario of checking the domain against lets say 3 or more possible values, then regex is a quick way to solve this.
You're welcome. I'm usually rather eager to solve things with regex myself. So I usually think again: "will a regex make that code better?" and try it without the regex. In ~50% of the cases, I keep the regex. For the sake of having an example, I feel it's justified. For live code, I would have removed it.
Just a coder and a dad. I love my family and I love to code!!!! started coding at 11, so I have 25 years under my belt. Still love learning about it every day. Black lives matter!
What is difficult to read it what is obvious to you strongly depends on what you're used to. Using logical gates is pretty common for example in Python.
So basically your judgement depends on your own bias for a certain style.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Three minor additions: you can omit the ternary in your case in favor of a logical gate and you should use
/regex/.test(string)
instead ofstring.match(/regex/)
if you don't want to do anything with the result and lastly, you don't want domains like localhost.mydomain.com to be interpreted as secure, so better make sure that regex captures the whole domain string - so now it looks like this:I would even think that using a direct comparison for the domain would be faster and more readable than the regex, so now it becomes:
Aw! you're so right! I should have used test vs match, also I should have added in $ for an exact match. I'll update this! Part of the fun was to show people they can resort to regex when doing multiple test against a single variable. If it was another scenario of checking the domain against lets say 3 or more possible values, then regex is a quick way to solve this.
Thank you for the great feed back.
You're welcome. I'm usually rather eager to solve things with regex myself. So I usually think again: "will a regex make that code better?" and try it without the regex. In ~50% of the cases, I keep the regex. For the sake of having an example, I feel it's justified. For live code, I would have removed it.
I like your clear view on this. I agree that the regex is over kill when we are only checking two values.
While this obviously works, I personally don't like this style of code.
Ok it takes up less screen real estate, but it's operation is not obvious.
Fundamentally code is a document and a document is useless if it is difficult or impossible to read.
What is difficult to read it what is obvious to you strongly depends on what you're used to. Using logical gates is pretty common for example in Python.
So basically your judgement depends on your own bias for a certain style.