How do I split a string by whitespace and ignoring leading and trailing whitespace into an array of words using a regular expression?

前端 未结 4 1907
陌清茗
陌清茗 2020-12-01 09:54

I typically use the following code in JavaScript to split a string by whitespace.

\"The quick brown fox jumps over the lazy dog.\".split(/\\s+/);
// [\"The\"         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 10:30

    Instead of splitting at whitespace sequences, you could match any non-whitespace sequences:

    "  The quick brown fox jumps over the lazy dog. ".match(/\S+/g)
    

提交回复
热议问题