RegExp Examples

by Joshua McNeese

JavaScript

var re = /abc/;

// its just an object
console.log(typeof re);

console.log(re.test("a")); // false
console.log(re.test("abcdefg")); // true

console.log(re.exec("a")); // null - no matches
console.log(re.exec("abcdefg")); // match array
// match array has the matched string
// the index of the matched string
// the input

var re = /boo(hoo+)(sob)*/i;

// match arrays here will include the grouped matches after the initial string
console.log(re.exec("boohoohoohoo"));
console.log(re.exec("boohoosobs"));