DEV Community

Discussion on: How do you regex?

Collapse
 
lexlohr profile image
Alex Lohr

I just write them down; ,usually they work. I comment more complex RegExp by splitting up the parts of it in a comment, e.g. for a simple example:

// Matches cookies in document.cookie
// $1: key `([^=]+)` one or more not `=`
// separator `=`
// $2: value `([^;]+)` one or more not `;`
// Modifier 'g' (global)
const cookieMatcher = /([^=]+)=([^;]+)/g;
Enter fullscreen mode Exit fullscreen mode