I'm writing an assembler parser using JavaScript (don't ask why)
Need to split words, but characters like . , : need to be separate array element
As a solution i can use .split(' ') and just check with .includes(/[,|.|:]/g) find index and push after that element, but I think there is a better solution for this task.
Example
input: 'mov al, bl'
output: ['mov', 'al', ',', 'bl']
You can use regular expressions in split() to include separator characters in final array.
let test = "mov al, bl"
let res = test.split(/([,\.:])/).map(el => el.trim().split(" ")).flat()
console.log(res)