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






你可能感興趣的文章

Web開發學習筆記03—Logical operators & Truthy、Falsy

Web開發學習筆記03—Logical operators & Truthy、Falsy

行為心理學/經濟學 科普書

行為心理學/經濟學 科普書

Multicast DNS (mDNS)

Multicast DNS (mDNS)






留言討論