menu

split method

The split method splits a string into an array by a delimiter (separator) in the form of regular expression.

The method takes a regex as the first parameter, and the maximum number of elements in the resulting array as the second optional parameter.

Syntax

string.split(regular expression, [limit]);

Example

Let's split the string into an array by the delimiter '-' or by the delimiter '+':

let str = 'a-b+c-d+e'; let res = str.split(/[-+]/); console.log(res);

The code execution result:

['a', 'b', 'c', 'd', 'e']

Example

Let's limit the number of elements in the resulting array:

let str = 'a-b+c-d+e'; let res = str.split(/[-+]/, 3); console.log(res);

The code execution result:

['a', 'b', 'c']

See also

  • the test method
    that checks a string
  • the match method
    that searches for matches in a string
  • the matchAll method
    that searches for all matches in a string
  • the exec method
    that performs a sequential search
  • the replace method
    that performs search and replacement
  • the search method
    that performs a search
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline