how can I separate by “,” or “-” or ” ” ?
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
if setting "/,- +/"
is not working, what should I do?
Advertisement
Answer
Your regular expression is basically saying, “match ,-
followed by at least one space”.
This would match ",- "
, for example.
What you want is a character group of ,
, -
and space (note that you need to escape -
there):
/[,- ]/