Skip to main content
Notice removed Needs detailed answers by Jamal
deleted 72 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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.

A very lazy oneliner:

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.

The 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, ")");
}
added 211 characters in body
Source Link
maaartinus
  • 13.5k
  • 1
  • 34
  • 73

A very lazy oneliner:

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.

A very lazy oneliner:

function dashesToParentheses(str) {
    return str.replace(/-/g, "(") + str.replace(/[^-]*-[^-]*/g, ")");
}

A very lazy oneliner:

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.

Notice added Needs detailed answers by Jamal
Source Link
maaartinus
  • 13.5k
  • 1
  • 34
  • 73

A very lazy oneliner:

function dashesToParentheses(str) {
    return str.replace(/-/g, "(") + str.replace(/[^-]*-[^-]*/g, ")");
}