Decode My String (Brackets) Taken from StackOverflow Question

Challenge

Given an encoded string in format n[s] where n is the amount of times s will be repeated, your task is to output the corresponding decoded string.


n will always be a positive number
s can not contain empty spaces
strings only contains letters from the alphabet
s length can be &gt;= 1
encoded format is number [ string ]
string can have nested encoded string number [ number [ string ] ]
return the same string if not encoded


Examples

2[a]                 =&gt; aa
3[2[a]]              =&gt; aaaaaa
2[b3[a]]             =&gt; baaabaaa
3[a]5[r3[c]2[b]]a    =&gt; aaarcccbbrcccbbrcccbbrcccbbrcccbba
a                    =&gt; a  // Not Encoded
3                    =&gt; 3  // Not Encoded
3a3[b]               =&gt; 3abbb
10[b20[a]]           =&gt; baaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaa
3[hello]             =&gt; hellohellohello


Other test cases

a[a] =&gt; a[a] 
a1[a] =&gt; aa
3[] =&gt;    // Nothing because are 3 empty string
2[2][a] =&gt; 2[2][a]




This is my own submission.



f=i=&gt;(r=/(\d+)\[([a-z]*)\]/g).test(l=i.replace(r,(m,n,s)=&gt;s.repeat(n)))?f(l):l

console.log(f("2[a]"))
console.log(f("3[2[a]]"))
console.log(f("2[b3[a]]"))
console.log(f("3[a]5[r3[c]2[b]]a"))
console.log(f("a"))
console.log(f("3"))
console.log(f("3a3[b]"))
console.log(f("10[b20[a]]"))
console.log(f("3[hello]"))






If more test cases needed please tell me.
QuadR, 17 + ≡ flag = 28 bytes



\d+\[\pL*]
∊(⍎⍵M∩⎕D)⍴⊂⍵M∩⎕A


Try it online!

Find:

 \d+\[\pL*] find digit(s) followed by bracketed Letters

Replace with:

⎕A uppercase Alphabet

 ⍵M∩ intersection of Match and that

 ⊂ enclose (to treat as a whole)

 (…)⍴ reshape to length:

  ⎕D Digits

  ⍵M∩ intersection of Match and that

  ⍎ execute (converts text to number)

 ∊ ϵnlist (flatten)



This is equivalent to the Dyalog APL function:

'\d+\[\pL*]'⎕R{∊(⍎⍵.Match∩⎕D)⍴⊂⍵.Match∩⎕A}⍣≡