I want to split string into array with fix number of char but the counting should be from backwards. In the following example I want to split the string into array with set of 3 char:
InputString = '1234567'
Wanted:
OutputArray= [1,234,567]
Tried:
InputString.match(/.{1,3}/g)
OutputArray = [123,456,7]
Use String#match with positive lookahead assertion regex.
var InputString = '1234567';
console.log(
InputString.match(/\d{1,3}(?=(\d{3})*$)/g)
)
Regex explanation here.
With String#split method with positive lookahead assertion for asserting position to split.
var InputString = '1234567';
console.log(
InputString.split(/(?=(?:\d{3})+$)/)
)
Regex explanation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With