如果一个字符串包含其中任何一个/,+,?,-,_,#
,我想查找并替换其中任何一个,分别_sls_,_pls_,_wht_,_dsh_,_usc_,_hsh_
这个。
示例:
'_sls_'=>'/','_pls_'=>'+','_wht_'=>'?','_dsh_'=>'-','_usc_'=>'_','_hsh_'=>'#'
Mary_land = Mary_usc_land
Mary+land = Mary_pls_land
将所有要替换的字符写到函数中并调用它
var string1 = "Mary_land";
var string2 = "Mary+lang";
var string3 = "Mary#lang";
var string4 = "Mary-lang";
var string5 = "Mary/lang?Maryland";
console.log(normalize(string1));
console.log(normalize(string2));
console.log(normalize(string3));
console.log(normalize(string4));
console.log(normalize(string5));
function normalize(str){
str = str.replace(/_/g,"_usc_");
str = str.replace(/+/g,"_pls_");
str = str.replace(///g,"_sls_");
str = str.replace(/#/g,"_hsh_");
str = str.replace(/-/g,"_dsh_");
str = str.replace(/?/g,"_wht_");
return str;
}