Regular Expression Match Groups

Regular expressions are incredibly powerful but can be difficult to maintain. They’re a skill you learn on the job and, when the suits walk by, make you look incredibly smart if you have a few up on your screen. How can we solve the maintainability problem? With a match groups, as Addy Osmani enlightened me […]

The post Regular Expression Match Groups appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Regular expressions are incredibly powerful but can be difficult to maintain. They’re a skill you learn on the job and, when the suits walk by, make you look incredibly smart if you have a few up on your screen. How can we solve the maintainability problem? With a match groups, as Addy Osmani enlightened me about last week:

Look at the ?<descriptor> pattern, with the descriptor being a meaningful name that you want to give to a give group. With the group usage, you can more intelligently handle match results:

const re = /(?\d{4})-(?\d{2})-(?\d{2})/;
const result = re.exec('2021-04-26');

// Deconstructing from result.groups
const { year, month, day } = result.groups;

// Using array syntax
const [, year, month, day] = result;

The only real downside of using this strategy is that most developers probably don’t know about it. You could also complain that it makes the regular expression longer. In the end, however, maintainability rules the day, and I love that Addy shared this tip with us!

The post Regular Expression Match Groups appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2021-04-26T14:25:17+00:00) Regular Expression Match Groups. Retrieved from https://www.scien.cx/2021/04/26/regular-expression-match-groups/

MLA
" » Regular Expression Match Groups." David Walsh | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/regular-expression-match-groups/
HARVARD
David Walsh | Sciencx Monday April 26, 2021 » Regular Expression Match Groups., viewed ,<https://www.scien.cx/2021/04/26/regular-expression-match-groups/>
VANCOUVER
David Walsh | Sciencx - » Regular Expression Match Groups. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/regular-expression-match-groups/
CHICAGO
" » Regular Expression Match Groups." David Walsh | Sciencx - Accessed . https://www.scien.cx/2021/04/26/regular-expression-match-groups/
IEEE
" » Regular Expression Match Groups." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/regular-expression-match-groups/. [Accessed: ]
rf:citation
» Regular Expression Match Groups | David Walsh | Sciencx | https://www.scien.cx/2021/04/26/regular-expression-match-groups/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.