Getting started with RegEx

by Kevin Kirchner

JavaScript

// Our string
var str = "Fat baby CAT is my best buddy little bat.";


// Two ways to write regular expressions
var exp = /at/;
/* var exp = new RegExp('at'); */


// Our replacement
var repl = 'og';


// A few ways to use regular expressions
/* var resultReplace = str.replace(exp, repl); */
/* var resultMatch = str.match(exp); */
/* var resultTest = exp.test(str); */
/* var resultExec = exp.exec(str); */


// Log the results to the console
console.log("str:    ", str);
/* console.log("replace:", resultReplace); */
/* console.log("match:  ", resultMatch); */
/* console.log("test:   ", resultTest); */
/* console.log("exec:   ", resultExec); */