A very lazy onelinerThe first term is obvious: Just replace all dashes by opening parentheses. The second replaces all dashes by closing parentheses while dropping everything else.
function dashesToParentheses(str) {
return str.replace(/-/g, "(") + str.replace(/[^-]*-[^-]*/g, ")");
}
There's nothing to say... but I was asked to. The first term is obvioius: Just replace all dashes by opening parentheses. The second replaces all dashes by closing parentheses while dropping everything else.