parsings strings: extracting words and phrases [JavaScript]

前端 未结 10 679
没有蜡笔的小新
没有蜡笔的小新 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:54

    This might be a very late answer, but I am interested in answering

    ([\w]+|\"[\w\s]+\")
    

    http://regex101.com/r/dZ1vT6/72

    Pure javascript example

     'The rain in "SPAIN stays" mainly in the plain'.match(/[\w]+|\"[\w\s]+\"/g)
    

    Outputs:

    ["The", "rain", "in", ""SPAIN stays"", "mainly", "in", "the", "plain"]
    

提交回复
热议问题