Reputation: 501
I have a string like this
const str = "'a' 'b' 'c'"
I want to split it and get in result array of strings
const arr = str.split(" ")
but in output I get:
["'a'", "'b'", "'c'"]
How can I get in output array without nested strings?
Desired result
["a", "b", "c"]
Upvotes: 0
Views: 89
Reputation: 39322
First remove '
from above string using .replace()
and then split it:
const str = "'a' 'b' 'c'";
const output = str.replace(/'/g, '').split(' ');
console.log(output);
Upvotes: 1
Reputation: 48357
You could use replace
method by passing a regex expression in combination with map
method by passing an arrow function as argument
.
const str = "'a' 'b' 'c'";
console.log(str.split(' ').map(el => el.replace(/'/g, "")));
Upvotes: 1