Replace all whitespace characters

前端 未结 10 1066
北荒
北荒 2020-12-04 07:39

I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript.
How to do so?

I tried:

str.replace(/ /gi, \"X\         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 08:12

    You could use the function trim

    let str = ' Hello World ';

    alert (str.trim());

    All the front and back spaces around Hello World would be removed.

提交回复
热议问题