Tokenize a string in JavaScript
This post will discuss how to tokenize a string in JavaScript.
To tokenize a string in JavaScript means to split the string into smaller units, such as words, symbols, or characters, based on some criteria or rules. Tokenization is often used for tasks such as natural language processing, text analysis, or lexical analysis. There are several ways to tokenize a string in JavaScript. Here are some of the most common and easy-to-use functions:
1. Using String.split() function
The String.split() is an instance function of the String object that splits a string into an array of substrings based on a separator. The separator can be a string or a regular expression that defines where to split the string. For example, if we want to tokenize a sentence into words, we can use (" ") separator to split by spaces. If we want to tokenize a word into characters, we can use an empty string ("") as the separator. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 |
// A sentence let str = "Hello World!"; // Tokenize the sentence into words using space as the separator let words = str.split(" "); console.log(words); // ["Hello", "World!"] // Tokenize the first word into characters using empty string as the separator let chars = words[0].split(""); console.log(chars); // ["H", "e", "l", "l", "o"] |
2. Using String.match() function
The String.match() is another instance function of the String object that matches a string against a regular expression and returns an array of the matched substrings. The regular expression can define what kind of substrings to match, such as letters, digits, symbols, or patterns. For example, if we want to tokenize a string into words, we can use a regular expression that matches one or more alphanumeric characters (/\w+/g). Here’s an example:
|
1 2 3 4 5 6 |
// A sentence let str = "Hello World!"; // Tokenize the sentence into words using \w+ as the regular expression let words = str.match(/\w+/g); console.log(words); // ["Hello", "World!"] |
3. Using String.replace() function
The String.replace() function replaces parts of a string that match a regular expression or a substring with another string or a function. We can use this function to tokenize a string by replacing the separators with some special characters, such as commas or brackets, and then parsing the resulting string into an array. For example, if we want to tokenize a sentence into words, we can use a space (" ") as the separator and replace it with a comma (","). Here’s an example:
|
1 2 3 4 5 6 |
// A sentence let str = "Hello World!"; // Tokenize the sentence into words by replacing space with comma let words = str.replace(/ /g, ",").split(","); console.log(words); // ["Hello", "World!"] |
That’s all about tokenizing a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)