Javascript: Simplifying many Regular Expression .replace() into fewer Regex calls

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前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏