javascript – Hoe restrict regexp in regexp group with 15 digits?

I have a regular expression that allows you to arrange it in bracket groups, but there, as I need a maximum of 15 characters, and the value of the code is calculated dynamically, I would like to know if it is possible to somehow limit the size of the characters in the regular expression to 15?

My regexp

let code = 3 // can be 1,2,3,4 etc

someValue
      .replace(/(?<!^)\+|[^\d+]+/g, '')
      .replace(new RegExp(`(\\d{${code}})(\\d)`), '($1) $2')
      .replace(/(\d{3})(\d)/, '$1-$2')
      .replace(/(\d{4})(\d)/, '$1-$2')
      .replace(/(-\d)/, '$1')

input
‘123456789101112131415’

output
‘(123)-456-7891-01112131415’

expected output ( max 15 digits )
‘(123)-456-7891-01112’

Nit:
Perhaps if you have a more elegant solution to achieve this result instead of what I provided, that would be great

Read more here: Source link