1.替换字符串中的重复字符:例:11111/1111111111/,将其中的‘/’去除
//控制'/'无法输入
function checkName(obj){
var reg = /\//g;
if (reg.test(obj.value)) {
obj.value = obj.value.replace(reg,'');
}
}
var s = "Hello. Regexps are fun." ;s = s.replace(/\./, "!" ); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\./g, "!" ); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”