DEV Community

Discussion on: How do you regex?

Collapse
 
xanderyzwich profile image
Corey McCarty

If at all possible, avoid using regex in code. If you must include regex in code then you should make efforts to explain what the different parts do. All of that said, I have come across some examples of entry validation in front-end where the best approach was to request the validations from the back-end depending on the locale, and then pass back regex strings. This keeps you from having hard coded regex in the front-end, but means that the back-end has a plethora of regex patterns stored based on the field being validated and the locale. For this think of a list of phone number validation regexes by locale code being in one place. This is an attempt to prevent sending information to the back-end to perform validation, but as a safety precaution, the back-end should still perform the validation to insure that there's been no tampering with the validation steps.

When I use regex it is usually for searching in my code or local file system, and I use Regexr as both a reference and a test tool. One instance that I used during Advent of Code was to find any commented/uncommented print calls in the code before commit.