parsings strings: extracting words and phrases [JavaScript]

前端 未结 10 687
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 19:20

I need to support exact phrases (enclosed in quotes) in an otherwise space-separated list of terms. Thus splitting the respective string by the space-character is not suffic

10条回答
  •  长情又很酷
    2020-12-01 19:58

    A simple regular expression will do but leave the quotation marks. e.g.

    'foo bar "lorem ipsum" baz'.match(/("[^"]*")|([^\s"]+)/g)
    output:   ['foo', 'bar', '"lorem ipsum"', 'baz']
    

    edit: beaten to it by shyamsundar, sorry for the double answer

提交回复
热议问题