js正则批量替换字符串-字符串批量替换

let str =  "sdfsfwwxxxfsfsxxdsdfwsdfxxxxa3sdswexxxxsdfsxxxxfa"

let newStr = str.replace(new RegExp(/xxx/g), "YYY") 

console.log(newStr)     //"sdfsfwwYYYfsfsxxdsdfwsdfYYYxa3sdsweYYYxsdfsYYYxfa"


注:若正则不加 g 只匹配第一项符合标准的字符串

你可能感兴趣的:(js)