Created
March 14, 2020 16:18
-
-
Save sese/2fc99d7e7205fbbcc8a436de3996f0e5 to your computer and use it in GitHub Desktop.
javascript tokenizer sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let print = (...arg) => { | |
console.log(...arg); | |
} | |
let longString = 'Acesta, este un string... mai lung'; | |
const sep = [' ', ',', '.', '!'] | |
let tokenizer = (str, seps = [' ']) => { | |
let tokens = [] | |
let token = '' | |
for (let i = 0; i < str.length; i++) { | |
let c = str[i] | |
if (seps.includes(c)) { | |
if (token) { | |
tokens.push(token) | |
token = '' | |
} | |
continue | |
} | |
token += c; | |
} | |
if (token) { | |
tokens.push(token) | |
} | |
return tokens; | |
} | |
console.log(tokenizer(longString, sep)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample application used to learn simple javascript commands: string, array, for, if