Split a string by whitespace, keeping quoted segments, allowing escaped quotes

前端 未结 4 1925
南方客
南方客 2020-11-28 09:27

I currently have this regular expression to split strings by all whitespace, unless it\'s in a quoted segment:

keywords = \'pop rock \"hard rock\"\';
keyword         


        
4条回答
  •  温柔的废话
    2020-11-28 09:50

    I would like to point out I had the same regex as you,

    /\w+|"[^"]+"/g
    

    but it didnt worked on empty quoted string such as :

    "" "hello" "" "hi"
    

    so I had to change the + quantifier by *. this gave me :

    str.match(/\w+|"[^"]*"/g);
    

    Which is fine.

    (ex: https://regex101.com/r/wm5puK/1)

提交回复
热议问题