Regular Expressions
Letâs look a little more closely at the pattern matching we have been doing. This has been achieved using regular expressions, which are supported by both JavaScript and PHP. They make it possible to construct the most powerful of pattern-matching algorithms within a single expression.
Matching Through Metacharacters
Every regular expression must be enclosed in slashes (/
). Within these slashes, certain characters
have special meanings; they are called
metacharacters. For instance, an asterisk (*
) has a meaning similar to what you have seen
if you use a shell or Windows Command prompt (but not quite the same).
An asterisk means, âThe text youâre trying to match may have any number
of the preceding characterâor none at all.â
For instance, letâs say youâre looking for the name âLe Guinâ and know that someone might spell it with or without a space. Because the text is laid out strangely (for instance, someone may have inserted extra spaces to right-justify lines), you could have to search for a line such as:
The difficulty of classifying Le Guin's works
So you need to match âLeGuin,â as well as âLeâ and âGuinâ separated by any number of spaces. The solution is to follow a space with an asterisk:
/Le *Guin/
Thereâs a lot more than the name âLe Guinâ in the line, but thatâs
OK. As long as the regular expression matches some part of the line, the
test
function returns a true value. What if itâs important to make sure the line contains ...
Get Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.