×

Pages

Labels

Search

×

Pages

×
×

Notice

The site is currently undergoing scheduled maintenance, and may not function correctly. Please come back later.

Advertisement

advertisement

How To Split A String Into An Array In Javascript

Split String Into 1 Character Size Pieces:
[quote]
var arr = str.match(/.{1,1}/g);
[/quote]
- .match() is used to search the string for regex matches, and return an array of the matches. "." means match any regular character. "{1,1}" means include the character "." 1 times per match.
- Replace "arr" with the desired name for the array.
- Replace "str" with the variable containing the string.

Split String Into N Size Pieces:
[quote]
var arr = str.match(/.{1,n}/g);
[/quote]
- .match() is used to search the string for regex matches, and return an array of the matches. "." means match any regular character. "{1,n}" means include the character "." 1 to "n" times per match. It's important that "1" is used so that if the end of the string doesn't have enough characters to meet the size of "n", it will still match, and therefore still be listed in the array, otherwise it would be removed.
- Replace "arr" with the desired name for the array.
- Replace "str" with the variable containing the string.
- Replace "n" with the size of each chunk.

Split String By Delimiter:
[quote]
var arr = str.split(/(?=delimter)/g);
[/quote]
- .split() is used to split a string into an array by a delimter which can be a regular expression (such as used in this example). "delimter" is the character or word to split before. "(?= )" is a positive lookahead being used to keep the delimiter in the result, because normally split removes it.
- Replace "arr" with the desired name for the array.
- Replace "str" with the variable containing the string.

Authors:James Daniel Marrs Ritchey
License:Ritchey Permissive License v8
Ritchey Permissive License v8:

You means the person, or legal entity exercising permissions granted by this license. Provider means a person (or legal entity) using permissions granted by this license to share the material. Permissions are revoked permanently upon breach of this license. Subject to the terms of this license, any person (or legal entity) is hereby granted otherwise irrevocable royalty-free permission to do anything with material provided under this license. The material is provided as is, without warranties of any kind, guarantees of any kind, or implied fitness for any purpose. The authors, owners, and providers will not be held responsible for anything caused by the material. When sharing the material with others you take on all responsibilities of and relating to consequences (including warranties, implied warranties, guarantees, liabilities, and damages), including those which would normally be the responsibility of the owner, between you and those directly and indirectly receiving the material from you. The material must remain solely under this license. This license is to be upheld in Canada, subject to the laws of Canada, as they were on April 21, 2019. You must be legally capable of being bound to all the requirements of this license, and by using the material you agree to be. The license text is provided under these terms.


Advertisment

advertisement
Copyright © James Daniel Marrs Ritchey.

Siteviews