如何设置 Lodash 的“_.truncate”的多个分隔符?

how could set multi separator of "_.truncate" of Lodash?

如何用“,”或“-”或“”分隔?

    let _r = _.truncate('hi-diddly-ho there, neighborino', {
          'length': 16,
          'separator': /,- +/
        });
        console.log(_r);   //need output: hi
    
    let _r = _.truncate('hi!diddly ho there, neighborino', {
          'length': 16,
          'separator': /,- +/
        });
        console.log(_r);   //need output: hi!diddly
    
    let _r = _.truncate('hi!diddly!ho,there, neighborino', {
          'length': 16,
          'separator': /,- +/
        });
        console.log(_r);   //need output: hi!diddly!ho

如果设置"/,- +/"不起作用,我该怎么办?

你的正则表达式基本上是说,“匹配 ,- 后跟至少一个 space”。

Check it on Regex101

这将匹配 ",- ",例如。

你要的是,-和space的字符组(注意这里要转义-):

/[,\- ]/

Check it on Regex101