Elli Zorro
Elli Zorro

Reputation: 501

How split string with nested strings

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

Answers (2)

Mohammad Usman
Mohammad Usman

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

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

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

Related Questions