Skip to main content
added 27 characters in body
Source Link
max890
  • 131
  • 3

JavaScript - 77 7575 72 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*[^[]*?)\]](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Edit: updated regex with Shaggy's recommendation

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*[^[]*?)\]](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

JavaScript - 77 75 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

JavaScript - 77 75 72 bytes

f=a=>a.replace(/(.*)\[([^[]*?)](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Edit: updated regex with Shaggy's recommendation

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^[]*?)](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

added 48 characters in body
Source Link
max890
  • 131
  • 3

#JavaScript - 77 75 bytes

JavaScript - 77 75 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

#JavaScript - 77 75 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

JavaScript - 77 75 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

deleted 4 characters in body
Source Link
max890
  • 131
  • 3

#JavaScript - 7777 75 bytes

f=a=>f=a=>a.replace(m=/(.*)\[([^\[\]]*)\](.)(.*)/.exec,(a),b,c,d,e)?f=>f(m[1]+m[2]b+c.repeat(m[3]d)+m[4]+e):a)

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>f=a=>a.replace(m=/(.*)\[([^\[\]]*)\](.)(.*)/.exec,(a),b,c,d,e)?f=>f(m[1]+m[2]b+c.repeat(m[3]d)+m[4]+e):a)

d.innerHTML=test.map(f).join("<br>");
<p id="d">

#JavaScript - 77 bytes

f=a=>(m=/(.*)\[([^\[\]]*)\](.)(.*)/.exec(a))?f(m[1]+m[2].repeat(m[3])+m[4]):a

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>(m=/(.*)\[([^\[\]]*)\](.)(.*)/.exec(a))?f(m[1]+m[2].repeat(m[3])+m[4]):a

d.innerHTML=test.map(f).join("<br>");
<p id="d">

#JavaScript - 77 75 bytes

f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

Snippet:

const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "before [in ]2after"];

const f=a=>a.replace(/(.*)\[([^\[\]]*)\](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e))

d.innerHTML=test.map(f).join("<br>");
<p id="d">

deleted 2 characters in body
Source Link
max890
  • 131
  • 3
Loading
deleted 7 characters in body
Source Link
max890
  • 131
  • 3
Loading
added 534 characters in body
Source Link
max890
  • 131
  • 3
Loading
Source Link
max890
  • 131
  • 3
Loading