Skip to content

Instantly share code, notes, and snippets.

@sese
Created March 14, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sese/2fc99d7e7205fbbcc8a436de3996f0e5 to your computer and use it in GitHub Desktop.
Save sese/2fc99d7e7205fbbcc8a436de3996f0e5 to your computer and use it in GitHub Desktop.
javascript tokenizer sample
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));
@sese
Copy link
Author

sese commented Mar 14, 2020

Sample application used to learn simple javascript commands: string, array, for, if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment