I\'d like to somehow replace the string sequence \"*\" with (*) using a regex in Javascript. Replacing things between quotes to be between opening and closing parenthesis.
Try something like:
str.replace(/"(.*?)"/g, function(_, match) { return "(" + match + ")"; })
Or more simply
str.replace(/"(.*?)"/g, "($1)")
Note the "non-greedy" specifier ?
. Without this, the regexp will eat up everything including double quotes up until the last one in the input. See documentation here. The $1
in the second fragment is a back-reference referring the first parenthesized group. See documentation here.