Viet Le Viet Le - 2 years ago 316
Javascript Question

Javascript Regex

Whats the difference between using [] and () in this situation?



var str = "Hel?lo the?re , Woul?d you like some ice?cream?";
var rExp = /[?+]/ig;
var rExp2 = /(\?+)/ig;

console.log(str.replace(rExp, ""));
console.log(str.replace(rExp2, ""));





Can I also get an explanation on when to use the other

Answer Source

You could throw in some +. [?+] checks for + as well.

var str = "Hel?lo t+he?re , Wo+ul?d you like some ice?cre+am?";
var rExp = /[?+]/ig;
var rExp2 = /(\?+)/ig;

console.log(str.replace(rExp, ""));
console.log(str.replace(rExp2, ""));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download