Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - how to split string?

0 contributions
9 points

This article focuses on different ways how to split string to array. JavaScript provides String.prototype.split method that can be used to do it.

1. Splitting string by text

Each string can be splitted in JavaScript with other string.

1.1. Split string by space example

In this example string with single space is used to spltit text to single words.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split(' '); // space character separator used

console.log(array);

1.2. Split string by some text example

This section shows how to split string by some other string.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split('is'); // this character sequence separates string

console.log(array);

1.3. Split string to array of characters example

In this apprach empty string is used to split main string into array of characters.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split(''); // empty separator used

console.log(array);

Note: this approach is strongly not recommended because of unicode characters that can be misinterpreted.

1.4. Split string by space with result size limit example

This section shows how to split string by space limiting output array size to two elements.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split(' ', 2); // maximum result array size will be 2

console.log(array);

2. Split string with regex (regular expression)

Each string can be splitted in JavaScript with regular expresion.

2.1. Split string by white character symbol example

In this example white character symbol is used to spltit text to single words.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split(/\b/); // begin or end of world separates string

console.log(array);

2.2. Split string by white character symbol with result size limit example

TIn this example white character symbol is used to spltit text to single words limiting output array size to two elements.

// ONLINE-RUNNER:browser;

var text = 'This is some text...';
var array = text.split(/\b/, 2); // maximum result array size will be 2

console.log(array);

3. Split string with Symbol.split (with splitter) example

Example how to use splitter is described in this article.

References

  1. String.prototype.split - MDN Docs 

0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ đź’» 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more