Regular expressions
Regular expressions from the basics in PHP and JavaScript: characters, classes and quantifiers, groups and backreferences, the preg_* functions in PHP, the RegExp object in JavaScript, Unicode and accented letters, and finally practice and the pitfalls that are easy to run into.
A regular expression is a short pattern that describes the text you are looking for – for example “one or two digits, a colon and two digits”. How to write such a pattern, what parts it consists of and how to use it in PHP and in JavaScript.
Parentheses in a pattern group its parts and at the same time remember what they matched. How to extract the day, month and year from a date, name groups, rearrange text when replacing and find doubled words.
PHP works with regular expressions through the preg_* functions, which are built on the PCRE library. How to write a pattern with delimiters and modifiers, which function to use for what, how to safely insert text from a user into a pattern and how to detect an error.
In JavaScript, a regular expression is a RegExp object and string methods such as match, replace or split work with it. How to write a pattern, what the individual flags do, which method to use when and why test() with the same pattern succeeds once and fails the next time.
As long as a text contains only English letters, regular expressions work without surprises. With accented letters, however, they count bytes instead of letters without the right modifier, and neither \w nor \b recognizes them. How to handle this in PHP and in JavaScript.
Techniques that come in handy in everyday work and pitfalls that are easy to run into: why .* takes more than it should, how to check the text before and after a match, how to validate a date and why one innocent-looking pattern can block a server.