0

I'm trying to turn a string into an array using RegExp. I have this string:

const str = "Hello, bold(username!). How are you?"

And I want that array:

["Hello", "bold(", "username!", ")", ". How are you?"]

I have this code:

const reg = /(bold\(|italic\(|\))/;
str.split(reg);

The return is correct, but when I try that one:

const str = "Hello, bold((username!)). How are you?"

The return is:

["Hello", "bold(", "(username!", ")", "", ")", ". How are you?"]

What can I do to get the result as below?

["Hello", "bold(", "(username!)", ")", ". How are you?"]

EDIT:

The array must have this structure:

[..., "bold(", inside_text, ")", ...]

where inside_text is every text inside main parentheses.

0 Answers0