DEV Community

Discussion on: Why Regular Expressions Are Super Powerful, But A Terrible Coding Decision

Collapse
 
nicozerpa profile image
Nico Zerpa (he/him) • Edited

Great article, Joseph!

As you said, if you're using regular expressions to extract data from JSON or HTML is the wrong tool for the job. I'd like to explain why.

Regular expressions are useful when the string in which you search has a regular structure. A good example is ISO 8601 dates, like "2021-10-07". Every single string with this format will begin with four digits for the year, the next two digits represent the month, and the last two are for the day. And there'll be two hyphens separating each group.

On the other hand, JSON and HTML are not regular. In the JSON example that Joseph posted in the article, if you add another property to the JSON, the regular expression won't find anything. But a JSON parser wouldn't have any problems getting the userID property.

I use regular expressions, especially when I have to find a very specific row on a database. In my code, I use them for very simple things. For example, if I search for content at the beginning or end of the string. I feel comfortable using them.