how can I separate by “,” or “-” or ” ” ?
JavaScript
x
18
18
1
let _r = _.truncate('hi-diddly-ho there, neighborino', {
2
'length': 16,
3
'separator': /,- +/
4
});
5
console.log(_r); //need output: hi
6
7
let _r = _.truncate('hi!diddly ho there, neighborino', {
8
'length': 16,
9
'separator': /,- +/
10
});
11
console.log(_r); //need output: hi!diddly
12
13
let _r = _.truncate('hi!diddly!ho,there, neighborino', {
14
'length': 16,
15
'separator': /,- +/
16
});
17
console.log(_r); //need output: hi!diddly!ho
18
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):
JavaScript
1
2
1
/[,- ]/
2