Viliami Viliami - 8 days ago 4
Javascript Question

Check if string contains character within a group

I've tried this (using javascript):

"[a][\"][\"2]".replace(/(\[)[\"](\])/g, "$1replaced$2")
//checks for double quote between [ and ]


and this is returned

"[a][replaced]["2]"


So the current regex only checks for a single double-quote character("), how do I check if a string contains a
"
character within the matched group (between
[
and
]
)?

.

So the outcome I'm looking for is changing from this:

[a]["]["2]


to

[a][replaced][replaced2]


basically replacing all double-quote characters within
[
and
]

Answer

Here's what you want

"[a][\"][\"2][asdf\"1234]".replace(/(\[)([^\]]*)(\])/g, function (match, leftBr, guts, rightBr) {
  return leftBr + guts.replace(/\"/g, "replaced") + rightBr;
});

It grabs the guts of every bracket group and removes the quotes.