Here goes two same ways to achive something. They both should do the same thing, but they don't.
Code: http://jsfiddle.net/8vjctofs/
a1 = function(str) {
return str.replace(new RegExp("[^a-zA-Z0-9\s]", "gi"), "");
}
a2 = function(str) {
return str.replace(/[^a-zA-Z0-9\s]/gi, "");
}
var string = "abc abc";
console.log(a1(string));
console.log(a2(string));
output:
abcabc
abc abc
Please, tell me why? Code is the same, except that first function creates regex, by new operator, second not.