Comment about Regular Expressions from PHP5 Unleashed by John Coggeshall
"Regular expressions (regex) are one of the black arts of practical computer programming. Ask any programmer, and chances are that he or she will, at some point, have had serious problems with them (or, even worse, avoided them altogether."
Character 1: forward slashes / /
Regular expressions start and end with a forward slash. That's how the computer knows that you are using a regular expression. This is very similar to how you surround a string with quotes.
String: "This is a string. It is surrounded by quotes."
Regular Expression: /test/
By using a regular expression as an argument in a string function, such as search(), you can find that pattern used. The regular expression listed above will find the first instance of the word "test" in a String.
Usually, regular expresions are stored in variables:
PHP: $FindString = /test/;
JavaScript: var FindString = /test/;
$FindString will also find a match in the word "protest" or "testament".
Character 2: global match g
Using the regular expression FindString, will only match the first "test" and then it will stop. If you need a way to find all the "test" instances, use "g" at the end of your regular expression.
Notice in the example, the "g" comes after the second /
JavaScript: var FindString = /test/g;
Character 3: ignore the case i
If you want to make a match with TEST, Test, test or any other cap combination, add "i" at the end of the regular expression
JavaScript: var FindString = /test/gi;
Character 4: for grouping, use square brackets []
Your regular expressions may be rather cryptic: /[^a-z\d ]/i. The forward slashes and the "i" have already been identified. Notice that the rest of the contents are inside [ ].
Character 5: Match a letter a-z and A-Z
If you want to find letters, it would take a long time to write out the whole alphabet. It would also make your regular expression very long. So, there is a short hand way to say you want all the letters.
PHP: $FindLetters = /a-z/;
JavaScript: var FindLetters =
/a-z/
Character 6: Match a digit (number) \d