1.0.0 • Published 7 years ago

match-replace v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

match-replace

Retrieves a new string when matching a pattern.

Usage

matchReplace(str, regexp, replacement)

Parameters

  • str input string
  • regexp a RegExp object or literal
  • replacement the string that replaces the matched regexp

The replacement string can include the following special replacement patterns:

PatternInserts
$$Inserts a "$".
$&Inserts the matched substring.
$`Inserts the portion of the string that precedes the matched substring.
$'Inserts the portion of the string that follows the matched substring.
$nWhere n is a positive integer less than 100, inserts the nth parenthesized submatch string.

Return value

A new string based on replacement when regexp has matched; false if if there were no matches.

Examples

import matchReplace from 'match-replace';

matchReplace('123456789', /(\n{2})(\n{3})(\n{4})/, '($0) $1-$2');
// returns "(12) 345-6789"

matchReplace('invalid data', /(\n{2})(\n{3})(\n{4})/, '($0) $1-$2');
// returns false