Javascript Split Space Delimited String and Trim Extra Commas and Spaces

后端 未结 6 2367
时光说笑
时光说笑 2021-02-19 15:14

I need to split a keyword string and turn it into a comma delimited string. However, I need to get rid of extra spaces and any commas that the user has already input.



        
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 16:01

    let query = "split me by space and remove trailing spaces and store in an array  ";
    let words = query.trim().split(" ");
    console.log(words)
    

    Output : [ 'split', 'me', 'by', 'space','and','remove', 'trailing', 'spaces', 'and', 'store', 'in', 'an', 'array' ]

提交回复
热议问题