

If you'd like to learn more about the Spread Operator - read our Spread Operator in JavaScript! In our context - we'd be most interested in expanding arrays (strings are arrays of characters).

The Spread Operator has several uses and is a widely-applicable operator in JavaScript. Additionally, the om() method supports emoji characters: let word = "It is sweet 😋" Ĭonsole.log(wordChars) // Split String into Array with the Spread Operator The major benefit of using om() instead of split() is that you don't have to bother with setting a delimiter - the constituent elements are just re-exposed and added to an array, rather than being converted explicitly.
#CONVERT STRING TO ARRAY JS HOW TO#
The big question was how to end it Was he there? Terrible occurrence." let sentences = text.match( /++/g) Īgain, if you're interested in learning more about Regular Expressions - read our Guide to Regular Expressions and Matching Strings in JavaScript! This is where we can use the match() method instead - which returns the matching patterns and their delimiters: let text = "What a big family it was! That would be a big help. Additionally, we have multiple whitespaces in the beginnings of the sentences, and there's an empty string in the end! This isn't to say that split() doesn't work well with Regular Expressions - but it is to say that splitting sentences out of text isn't solved well by split(). However, the delimiters are lost! We split on them and in the process, remove them from the output. The big question was how to end it Was he there? Terrible occurrence." let sentences = text.split( //) Ĭonsole.log(sentences) // Pattern matching is where Regular Expressions excel! Let's split a string on each sentence, with any of these ending delimiters: let text = "What a big family it was! That would be a big help. Each of these are valid sentences, but we'd have to perform multiple splits to match all of them, if we were to use single characters or strings. ), exclamation mark ( !), a question mark ( ?) or three dots (. For instance, say you'd like to break a string into sentences.


You can't match by multiple delimiters, unless you use a Regular Expression. One of the major downsides of using single characters or even entire strings is that the approach is fairly rigid. We could split it on each whitespace (breaking it down into words), or on every character, or any other arbitrary delimiter, such as 'p': // Split string using a whitespace let array1 = quote.split( ' ') Ĭonsole.log(array1) // // Splitstring using an empty string (on each character) let array2 = quote.split( '') Ĭonsole.log(array2) // // Split string using a specific character let arra圓 = quote.split( 'p') Ĭonsole.log(arra圓) // If you'd like to learn more about Regular Expressions - read our Guide to Regular Expressions and Matching Strings in JavaScript!įor example, suppose we have a string: let quote = 'I am unstoppable!' The pattern/divider/delimiter is the first parameter in the method's call and can be a regular expression, a single character, or another string. The split() method is used to divide a string into an ordered list of two or more substrings, depending on the pattern/divider/delimiter provided, and returns it. In this guide, learn how to convert a String to an Array in JavaScript, with the split(), Object.assign(), om() methods and spread operator, as well as when to use which. Whether you're breaking a word down into its characters, or a sentence into words - splitting a string into an array isn't an uncommon operation, and most languages have built-in methods for this task. These sequences, are ultimately, arrays, and converting between the two structures typically is both simple and intuitive. Textual data is typically stored through sequences of characters - strings.
