How to add spaces between every character in a string?

后端 未结 4 1521
盖世英雄少女心
盖世英雄少女心 2020-12-06 19:07

I am trying to create a function that inserts spaces between the characters of a string argument then return a new string which contains the same characters as the argument,

4条回答
  •  囚心锁ツ
    2020-12-06 19:46

    That's quite easy... just call the replace method on the string as follow...

    var str = "Hello";
    console.info(str.replace(/\B/g, " ");
    

    What am I doing here is replacing on non-word boundary which is inside the word. It's just reverse of the word boundary denoted by "\b", which is around the word; think it as if you are matching the border of the word.

提交回复
热议问题