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"
2
Answers
You can match with a regex and limit your count using
{11}
.Here is a function that uses
String.prototype.slice
:You can easily reach the desired result using substring() function:
Or slice() as an option:
The only difference between these methods that you can concern about is the way they process negative values:
substring()
treats them as zeros whileslice()
makes an offset from the end of the string. Since 11 is a positive digit, it won’t make any difference for you.