Glob Expressions

JavaScript carries much the same suite of regular expression tools as Perl.

These three constitute our globs.

Ordinarily we think of only the first: we might "Find & Replace" all occurences of 'foo' with 'fo'--a linear process. Quantifiers add another dimension (exponentiality) and backreferences bring in the concepts of programming.

You must learn how a match is made.

/he/gi

/ and / are the quotes surrounding the string of two characters, h, e. Flags gi add functions for a global and case-insensitive search.

/.*\.jpe?g/g

Traditional globbing uses the * character, for example, *.jp*g. JavaScript uses a more powerful combination, the wildcard dot (.), and the greedy matcher (*): any number of any characters.

/[aeiou]+/gi

Quantifiers specify options and ranges for possible matches. One or more vowels: [aeiou]+

/[^\w\s]$/

Meta-characters specify predefined values. Trying to match end punctuation, we have "not a letter or a space; must be at the end:" [^\w\s]$

/^\s+$/

Matching an empty line, beginning to end nothing but space: ^\s+$

/(\S+:\/{2}\S+)/g

Finding a URL, assuming space on either side. Our backreferences "()" memorize the address, so we could then say <a href="$1">$1</a> to form a link. Note the backslash used to escape the quote mark /.