-2

Let's say I have this value 'BERNY564567JH89E'. How would I split the value into different strings, after the 11th character, which in this case would be 7. So for example, in the end I would have the following returned:

const orignalValue = "BERNY564567JH89E"

const splitValueOne = "BERNY564567"
const splitValueTwo = "JH89E"

1 Answers1

1

You can match with a regex and limit your count using {11}.

const
  orignalValue = 'BERNY564567JH89E',
  [match, group1, group2] = orignalValue.match(/([a-z0-9]{11})([a-z0-9]*)/i) ?? [];

console.log({ group1, group2 }); // { "group1": "BERNY564567", "group2": "JH89E" }

Here is a function that uses String.prototype.slice:

const
  orignalValue = 'BERNY564567JH89E',
  splitAtIndex = (str, index) => [str.slice(0, index), str.slice(index + 1)],
  [head, tail] = splitAtIndex(orignalValue, 11);

console.log({ head, tail }); // { "head": "BERNY564567", "tail": "JH89E" }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132