How can I split a JavaScript string by white space or comma?

前端 未结 6 529
遥遥无期
遥遥无期 2020-12-12 15:10

If I try

\"my, tags are, in here\".split(\" ,\")

I get the following

[ \'my, tags are, in here\' ]

Wherea

6条回答
  •  遥遥无期
    2020-12-12 15:54

    String.split() can also accept a regular expression:

    input.split(/[ ,]+/);
    

    This particular regex splits on a sequence of one or more commas or spaces, so that e.g. multiple consecutive spaces or a comma+space sequence do not produce empty elements in the results.

提交回复
热议问题