Javascript replace space with regex


Jan 08, 2022
\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






你可能感興趣的文章

SQL-injection lab(2)

SQL-injection lab(2)

D25_ ALG101-Unit 5-LIOJ 1046, 1034

D25_ ALG101-Unit 5-LIOJ 1046, 1034

來學 React 吧之二_React 重要概念 useEffect

來學 React 吧之二_React 重要概念 useEffect






留言討論