Remove comma out of square brackets - Regex

Asked

Viewed 672 times

3

I am trying to remove the comma that is located outside the brackets of the following excerpt:

2||Azul||Cor||["#1983ff", "#1983ff"],3||Amarelo||Cor||["#fff73d"]

I need the comeback to be that way:

2||Azul||Cor||["#1983ff", "#1983ff"]3||Amarelo||Cor||["#fff73d"]

Can any ninja in regex give me a hand in this? (The above section has no variation, always turn this way) Thanks.

  • You want a Regular Expression that selects everything but the comma out of the brackets, or select the comma out of the brackets and delete it?

  • @R.Santos Select the comma outside the brackets and eliminate it.

  • And the search pattern will always be the example?

  • Yes, it will be the same.

  • You’ll get more accurate answers if you tell us what in the section you posted varies. Who knows a few more examples?

  • I edited the question, actually there is not much to add. Basically that would be.

Show 1 more comment

2 answers

6


Assuming your return will have this format, you can simply:

var retorno =  string.replace("],","]");
  • 2

    Good, now it’s settled +1. .split('],').join('') would also give.

2

A simpler way to remove the comma by ignoring the rule of square brackets in the sentence, using regular expression, as below, it replaces: (comma + number) /first house, by (number) / second box:

var string = '2||Azul||Cor||["#1983ff", "#1983ff"],3||Amarelo||Cor||["#fff73d"]';

var rtn = string.replace(/(,)([0-9]+)/gi,'$2');

console.log(rtn);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.