How to remove extra white spaces using javascript or jquery?

后端 未结 4 2217
粉色の甜心
粉色の甜心 2021-02-05 04:16

I got HTML element contains this:

    
P6C245RO
4条回答
  •  萌比男神i
    2021-02-05 04:41

    For the string

    "    this  is my   string       "
    

    You probably want to make the excessive spaces single spaces, but remove the leading and trailing spaces entirely. For that, add another .replace

    s.replace(/\s+/g, " ").replace(/^\s|\s$/g, "");
    

    For

    "this is my string"
    

    Update

    s.replace(/\s+/g, " ").trim()
    

    Thanks, @Pier-Luc Gendreau

提交回复
热议问题