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






Related Posts

每日心得筆記 2020-06-25(四)

每日心得筆記 2020-06-25(四)

Day01 聊聊對話機器人與LINE Developers

Day01 聊聊對話機器人與LINE Developers

瀏覽器運作原理 速記

瀏覽器運作原理 速記






Comments