Regular Expressions Basics
Regular Expressions Basics
Regular Expressions Basics
Flexible patterns
Simple patterns Advanced expressions
Basic Syntax
Position Symbols
^ Matches the beginning of a string $ Matches the end of a string \b Matches a word boundary \B Matches a non-word boundary
Literal Symbols
alphanumeric All alphabetical and numeric characters match themselves (e.g. /2 apples/) \n New line \r Carriage return \t Horizontal tab \f Form feed \v Vertical tab \xdd Hex numbers \uXXXX Unicode representation of characters \ Escape special characters used as symbols: \$
Character Classes
Can be negated: [^abc] . (any) Matches any character except new line terminators \w (word) Matches alphanumeric equivalent to [a-zA-Z0-9_] \W (non-word) Matches non-word characters equivalent to
\d Matches any digit equivalent to [0-9] \D Matches any non-digit equivalent to [^0-9] \s Matches any white space equivalent to [ \t\r\n\v\f] \S Matches any non-space equivalent to [^ \t\r\n\v\f]
[^a-zA-Z0-9_]
Repetition symbols follow other symbols or patterns {x} Matches exactly x occurrences {x,} Matches x or more occurrences {x,y} Matches x to y (inclusive) occurrences * Matches 0 or more occurrences equivalent to {0,} + Matches 1 or more occurrences equivalent to {1,} ? Matches 0 or 1 occurrences equivalent to {0,1} [a-z]+ [A-Z]* L?evi
() are used to group characters toghether: (hubba\s)+ | is used as OR operator to define an alternation:
(ab)|(cd)|(de) (a|c) \n where n is 0 to 9, matches a previous group counted from left (\w+)\s+(\d+)\s+\2\1
i : ignore case make the expression case-insensitive g : global search searches for all occurrences of the pattern not just the first m : multiline changes the meaning of ^ and $ symbols between matching beginning and end of line and matching beginning and end of string
Pattern switches are used as parameters when constructing a RegExp object or at the end of the literal expression: new RegExp("JavaScript", "gi") /JavaScript/gi
JavaScript Usage
String methods: match( RegExp ): Array replace( RegExp, String ): String split( RegExp ): Array search( RegExp ): Number