Name
String
Strings support four convenience methods for pattern matching. Each method takes a pattern
argument, which may be a RegExp
object, or a string containing a regular expression pattern.
Methods
search(
pattern
)
Match
pattern
against the string, returning either the character position of the start of the first matching substring or-1
.replace(
pattern
,replacement
)
Search the string for a match of
pattern
, and replace the matched substring withreplacement
. Ifpattern
has global mode set, all matches ofpattern
are replaced. The replacement string may have$
n
constructs that are replaced with the matched text of then
th capture group inpattern
.match(
pattern
)
Match
pattern
against the string, returning either an array or-1
. Element 0 of the array contains the full match. Additional elements contain submatches from capture groups. In global (g
) mode, the array contains all matches ofpattern
with no capture group submatches.split(
pattern
,limit
)
Return an array of strings broken around
pattern
. Iflimit
is included, the array contains at most the firstlimit
substrings broken aroundpattern
. Ifpattern
contains capture groups, captured substrings are returned as elements after each split substring.
Get Regular Expression Pocket Reference, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.