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.
You could try something like
replace(/"(.*?)"/g, "($1)")
Example
"this will be \"replaced\"".replace(/"(.*)"/, "($1)")
=> this will be (replaced)
"this \"this\" will be \"replaced\"".replace(/"(.*?)"/g, "($1)")
=> this (this) will be (replaced)
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.