Skip to content

JavaScript String API

JavaScript String 方法,用于分割、替换、匹配和转换文本。

1 class · 11 methods

String

11 methods

UTF-16 码单元的不可变序列。

str.split(separator?, limit?)

将字符串分割为子字符串数组。

Parameters

NameTypeDescription
separatorstring | RegExp分隔符(字符串或正则表达式)。
limitnumber子字符串的最大数量。

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)

返回一个新字符串,替换 pattern 的第一个匹配。要替换所有匹配请使用 replaceAll。

Parameters

NameTypeDescription
patternstring | RegExp要匹配的模式。
replacementstring | 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)

返回字符串与正则表达式匹配的结果。

Parameters

NameTypeDescription
regexpRegExp正则表达式(字符串会被强制转换)。

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()

返回一个新字符串,移除两端的空白字符。

Returns

string

Example

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

从起始位置用 padString 填充字符串,直到达到 targetLength。

Parameters

NameTypeDescription
targetLengthnumber最终长度。
padStringstring填充字符串(默认为空格)。

Returns

string

Example

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

从末尾用 padString 填充字符串,直到达到 targetLength。

Parameters

NameTypeDescription
targetLengthnumber最终长度。
padStringstring填充字符串(默认为空格)。

Returns

string

Example

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

如果在字符串中找到 searchString 则返回 true。

Parameters

NameTypeDescription
searchStringstring要搜索的子字符串。
positionnumber起始位置(默认 0)。

Returns

boolean

Example

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

如果字符串以 searchString 开头则返回 true。

Parameters

NameTypeDescription
searchStringstring要检查的前缀。
positionnumber起始位置(默认 0)。

Returns

boolean

Example

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

如果字符串以 searchString 结尾则返回 true。

Parameters

NameTypeDescription
searchStringstring要检查的后缀。
lengthnumber将字符串视为长度为 length 个字符。

Returns

boolean

Example

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

返回由原字符串重复 count 次组成的新字符串。

Parameters

NameTypeDescription
countnumber重复次数。

Returns

string

Example

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

返回从 start 到 end(不包含 end)的子字符串。

Parameters

NameTypeDescription
startnumber起始索引。
endnumber结束索引(默认为 length)。

Returns

string

Example

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