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 with replacement. If pattern has global mode set, all matches of pattern are replaced. The replacement string may have $n constructs that are replaced with the matched text of the nth capture group in pattern.

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 of pattern with no capture group submatches.

split(pattern, limit)

Return an array of strings broken around pattern. If limit is included, the array contains at most the first limit substrings broken around pattern. If pattern 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.