Skip to content

JavaScript String API

JavaScript String methods for splitting, replacing, matching and transforming text.

1 class · 11 methods

String

11 methods

Immutable sequence of UTF-16 code units.

str.split(separator?, limit?)

Split the string into an array of substrings.

Parameters

NameTypeDescription
separatorstring | RegExpDelimiter (string or regex).
limitnumberMaximum number of substrings.

Returns

string[]

Example

javascript
'a,b,c'.split(',')        // ['a', 'b', 'c']
'hello'.split('')         // ['h','e','l','l','o']
'a-b-c'.split('-', 2)     // ['a', 'b']
str.replace(pattern, replacement)

Return a new string with the first match of pattern replaced. Use replaceAll for all matches.

Parameters

NameTypeDescription
patternstring | RegExpPattern to match.
replacementstring | FunctionReplacement string or function.

Returns

string

Example

javascript
'hello world'.replace('world', 'JS')  // 'hello JS'
'a-b-c'.replace('-', '_')              // 'a_b-c'
'a-b-c'.replace(/-/g, '_')             // 'a_b_c'
str.match(regexp)

Return the result of matching a string against a regular expression.

Parameters

NameTypeDescription
regexpRegExpRegular expression (string is coerced).

Returns

Array | null

Example

javascript
'2024-01-15'.match(/(\d{4})-(\d{2})-(\d{2})/)
// ['2024-01-15', '2024', '01', '15']
'abc'.match(/\d/)  // null
str.trim()

Return a new string with whitespace removed from both ends.

Returns

string

Example

javascript
'  hi  '.trim()  // 'hi'
'\n hello\n'.trim()  // 'hello'
str.padStart(targetLength, padString?)

Pad the string from the start with padString until it reaches targetLength.

Parameters

NameTypeDescription
targetLengthnumberFinal length.
padStringstringPadding string (default space).

Returns

string

Example

javascript
'5'.padStart(3, '0')     // '005'
'abc'.padStart(6, '*')   // '***abc'
str.padEnd(targetLength, padString?)

Pad the string from the end with padString until it reaches targetLength.

Parameters

NameTypeDescription
targetLengthnumberFinal length.
padStringstringPadding string (default space).

Returns

string

Example

javascript
'5'.padEnd(3, '0')     // '500'
'abc'.padEnd(6, '*')   // 'abc***'
str.includes(searchString, position?)

Return true if searchString is found within the string.

Parameters

NameTypeDescription
searchStringstringSubstring to search for.
positionnumberStart position (default 0).

Returns

boolean

Example

javascript
'hello world'.includes('world')  // true
'hello'.includes('a')             // false
str.startsWith(searchString, position?)

Return true if the string starts with searchString.

Parameters

NameTypeDescription
searchStringstringPrefix to check.
positionnumberStart position (default 0).

Returns

boolean

Example

javascript
'hello'.startsWith('he')  // true
'hello'.startsWith('el', 1)  // true
str.endsWith(searchString, length?)

Return true if the string ends with searchString.

Parameters

NameTypeDescription
searchStringstringSuffix to check.
lengthnumberConsider the string as length characters long.

Returns

boolean

Example

javascript
'hello'.endsWith('lo')  // true
'hello'.endsWith('ell', 4)  // true
str.repeat(count)

Return a new string consisting of count copies of the original.

Parameters

NameTypeDescription
countnumberNumber of copies.

Returns

string

Example

javascript
'ab'.repeat(3)  // 'ababab'
'-'.repeat(5)   // '-----'
str.substring(start, end?)

Return the substring from start to end (end excluded).

Parameters

NameTypeDescription
startnumberStart index.
endnumberEnd index (default length).

Returns

string

Example

javascript
'hello'.substring(1, 3)  // 'el'
'hello'.substring(2)      // 'llo'