Javascript Split Space Delimited String and Trim Extra Commas and Spaces

后端 未结 6 2369
时光说笑
时光说笑 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:00

    If you just want to split, trim and join keeping the whitespaces, you can do this with lodash:

    // The string to fix
    var stringToFix = "The Wizard of Oz,Casablanca,The Green Mile";
    
    // split, trim and join back without removing all the whitespaces between
    var fixedString = _.map(stringToFix.split(','), _.trim).join(' == ');
    
    // output: "The Wizard of Oz == Casablanca == The Green Mile"
    console.log(fixedString);

提交回复
热议问题