How can I test if a RegEx matches a string exactly?
var r = /a/; r.test(\"a\"); // returns true r.test(\"ba\"); // returns true testExact(r, \"ba\")
Either modify the pattern beforehand so that it only matches the entire string:
var r = /^a$/
or check afterward whether the pattern matched the whole string:
function matchExact(r, str) { var match = str.match(r); return match && str === match[0]; }