Dec
18
Quick quide to regular expressions in JavaScript
1. The form of an expression:
var a = /dog/gim
//dog = the expression to match, g = search all matches, i = case insensitive, m=multiple line
The cool thing is that /dog/ is an object, so you can, for example, do something like /dog/.test(something)
2. Common Functions
a.test(string_to_test); //returns boolean. Ex: /cat/i.test(”categoty”) will return true
a.exec(string_to_test) <=> string_to_test.match(a) //returns array with matches
string_to_test.replace(a, something_to_replace_with) // replaces a with somtehing_to_replace_with
Furthermore, string_to_test.replace(a, function(blabla) { return string_to_replace_with }) -> blabla is in fact a when inside the generic function
a.split(delimiter); // split string a in parts, by the delimiter; Ex. “i:am:cool”.split(”:”) will return an array with 3 values (i, am, cool)
3. Character classes
var reBatCatRat = /[bcf]art/gi; // will match bart, cart, fart
[] means match one of the chars in the paranthesis
/[^bcf]art/gi // will match anything but bart, cart, fart
{m,n} means minumum m occurances, but no more than n
For example /s{0,1}/ is equivalent to /s?/
4. Greedy: ? , * or + (starts from the end of the string and eliminates the last character. The match is erased and the process is repeated)
Reluctant ??, *? or +? (starts from the begining and erases the match)
For example,
var sToMatch = “First second third fourth fifth sixth”;
var reWords = /(\w+?)/g; //would match f,i,r,s…
var reWords = /(\w+)/g; //would match first, second… because it starts from the end and eliminates one character at a time, so when reaching “first ” it elminates the ” ” and the whole word is matched (it contains only \w chars (meaning no spaces and stuff)). In the first example, it starts with “a” witch matches, it erases it and continues…
var arrWords = sToMatch.match(reWords);
5. Grouping (dog){2} -> dogdog; (dog)? one or more occurances of dog
/([bd]ad?)*/; -> match zero or more occurrences of “ba”, “da”, “bad”, or
“dad”
/(mom( and dad)?)/; -> match “mom” or “mom and dad”
6. Expand the String obj to allow trimming:
String.prototype.trim = function ()
{
var reExtraSpace = /^\s+(.*?)\s+$/; // I think it would be better written as var reExtraSpace = /^\s*(.*?)\s*$/; - yup!!!
return this.replace(reExtraSpace, “$1”);
};
$1 is can be used inside replace, or inside the regexp. It is the number of the group “()”
for example var reMatch = /(\d{3}) (\d{4})/; $1 = (\d{3}) and $2 = (\d{4})
var sNew = sToChange.replace(reMatch, “$2 $1”); Replaces the first found group with the second and the second with the first
for example var reMatch = /(dog)\1/; matches dogdog (\1 means the group (dog)
7. “or” ; for example /(dog|cow)/ matches dog or cow. $1 will either be dog or cow
8. Non-capturing groups
var reNumbers = /blabla(?:\d+)/; “?:” makes the group not output $1, so the overhead of this dissapears. Furthermore, it can be used inside replace, without the worry that “aaa$1″ will replace with “aaa(…)”
9. Lookaheads, match only if a group of characters appears after another one.
/(bed(?=room))/ -> matches bed only!!!! if it’s followed by room
/(bed(!=room))/ -> matches bed only!!!! if it’s not followed by room
10. Instance properties
var reB = /b/g;
reB.lastIndex = last found index
11. Static properties
input = the whole string
lastMatch = the last match ![]()
lastParen = the last matched group
leftContext, rightContext = the left/right sub-string before/after the match
Comments
Leave a Reply







