[Solved] Split characters and numbers from a string to array Javascript [closed]


try this:

const reg = /([0-9.]+)(?![0-9.])|([a-z]+)(?![a-z])/gi

console.log("30000kg , 400lbs".match(reg)); // --> [ "30000", "kg", "400", "lbs" ]
console.log("30,000kg , 400lbs".match(reg)); // --> [ "30", "000", "kg", "400", "lbs" ]
console.log("30.000kg,400lbs".match(reg)); // --> [ "30.000", "kg", "400", "lbs" ]
console.log("30.000KG.400LBS".match(reg)); // --> [ "30.000", "KG", ".400", "LBS" ]

alternatively, if you want to trim the ‘.’ in case 4

const reg = /(?:[^.]([0-9.]+)(?![0-9.]))|(?:([a-z]+)(?![a-z]))/gi

console.log("30.000KG.400LBS".match(reg)); // --> [ "30.000", "KG", "400", "LBS" ]

7

solved Split characters and numbers from a string to array Javascript [closed]