4 ms·
And use the whitespace "trick" (usually via a metacharacter). This: HEADER_PARAM = /\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/ is not as useful as t
by brigandish 5y ago
And use the whitespace "trick" (usually via a metacharacter).
This:
HEADER_PARAM =
/\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/
is not as useful as this:
/
\s* # Maybe whitespace at the beginning
[\w.]+ # Header key
= # Equals (yes:)
(?: # Header value from here
[\w.]+ # Just about anything
| # or
" # it could be wrapped in quotes
(?:
[^"\\] # Not quotes or backslashes
|
\\. # Sometimes escaped characters occur
)* # Could even be an empty string
"
)? # Maybe they didn't supply a value
\s*
/x
If you can use interpolation with your regexes you can extend this idea into (largely) self-documenting regexes.
- jollybean 5y agoYes, this should be the best practice.