How to preserve separators in the result of String.prototype.split() (#tilPost)

Today, I found a tiny JavaScript gem that may come in handy in the future. You probably know the string method split().
Call it on a string, define a separator and receive an array of its substrings.
const string = “Hello party peop…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today, I found a tiny JavaScript gem that may come in handy in the future. You probably know the string method split().

Call it on a string, define a separator and receive an array of its substrings.

const string = "Hello party people!";
console.log(string.split(' '));
// Array(3) [ "Hello", "party", "people!" ]

The first function parameter, the separator, can be a string value but also a regular expression. By using a regular expression you can devide the original string dependending on different separators.

const string = "Hello_party-people!";
console.log(string.split(/[-_]/));
// Array(3) [ "Hello", "party", "people!" ]

No matter if your separator is a string value or regular expression, its value is usually not included in the resulting array. MDN states this functionality as follows:

When found, separator is removed from the string, and the substrings are returned in an array.

But here's the JavaScript trivia: if you use a regular expression as the separator and this regular expression includes capturing paratheses (( and )), the matching values are included in the result. 😲

const string = "Hello_party-people!";
console.log(string.split(/([-_])/));
// Array(5) [ "Hello", "_", "party", "-", "people!" ]

I wasn't aware of this behavior, and I bet it can replace some complex regular expression logic!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-01-22T23:00:00+00:00) How to preserve separators in the result of String.prototype.split() (#tilPost). Retrieved from https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/

MLA
" » How to preserve separators in the result of String.prototype.split() (#tilPost)." Stefan Judis | Sciencx - Saturday January 22, 2022, https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/
HARVARD
Stefan Judis | Sciencx Saturday January 22, 2022 » How to preserve separators in the result of String.prototype.split() (#tilPost)., viewed ,<https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to preserve separators in the result of String.prototype.split() (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/
CHICAGO
" » How to preserve separators in the result of String.prototype.split() (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/
IEEE
" » How to preserve separators in the result of String.prototype.split() (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/. [Accessed: ]
rf:citation
» How to preserve separators in the result of String.prototype.split() (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2022/01/22/how-to-preserve-separators-in-the-result-of-string-prototype-split-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.