Here's the most obvious example:
"madhur".replace(/(madhur)?/, "$1 ahuja"); // returns "madhur ahuja"
"madhur".replace(/(?:madhur)?/, "$1 ahuja"); // returns "$1 ahuja"
Backreferences are stored in order such that the first match can be recalled with $1
, the second with $2
, etc. If you capture a match (i.e. (...)
instead of (?:...)
), you can use these, and if you don't then there's nothing special. As another example, consider the following:
/(mad)hur/.exec("madhur"); // returns an array ["madhur", "mad"]
/(?:mad)hur/.exec("madhur"); // returns an array ["madhur"]