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






你可能感興趣的文章

〈 AI 學習筆記 〉Automated Machine Learning 簡介與Auto-Sklearn實作紀錄

〈 AI 學習筆記 〉Automated Machine Learning 簡介與Auto-Sklearn實作紀錄

Longest Common Prefix

Longest Common Prefix

使用powershell建立hyper-v的虛擬機器(windows11)

使用powershell建立hyper-v的虛擬機器(windows11)






留言討論