Replace all whitespace characters

前端 未结 10 1042
北荒
北荒 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:09

    If you use

    str.replace(/\s/g, "");
    

    it replaces all whitespaces. For example:

    var str = "hello my world";
    str.replace(/\s/g, "") //the result will be "hellomyworld"
    

提交回复
热议问题