I have the following javascript function that uses a lot of regex. Is there a way I can simplify this?
function encode(str){
if(typeof str==='number'){
return str;
}
if(typeof str!=='string'){
return '';
}
var enc=str;
enc=enc.replace(/_/g,'_u'); //underscore
enc=enc.replace(/(\r\n|\r|\n)/g,'_r'); //return
enc=enc.replace(/&/g,'_a'); //ampersand
enc=enc.replace(/\\/g,'_b'); //backslash
enc=enc.replace(/:/g,'_c'); //colon
enc=enc.replace(/"/g,'_d'); //double quote
enc=enc.replace(/=/g,'_e'); //equals
enc=enc.replace(/€/g,'_4'); //euro
enc=enc.replace(/\>/g,'_g'); //greater than
enc=enc.replace(/#/g,'_h'); //hash
enc=enc.replace(/'/g,'_i'); //inverted comma
enc=enc.replace(/\Ultimately What is your condition.You want to replace all the special characters with some ur stuff.Isn't it ?
以上就是Javascript: Simplifying many Regular Expression .replace() into fewer Regex calls的详细内容,更多请关注web前端其它相关文章!