Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

靜態、類別、實例方法(method)/變數

靜態、類別、實例方法(method)/變數

畢業多年後再回到杜鵑花節顧攤,是什麼感覺?

畢業多年後再回到杜鵑花節顧攤,是什麼感覺?

漫步華爾街

漫步華爾街






留言討論