Regex string match and replace that honors boundaries for targets prepended and/or appended with non-word characters.
$ npm install --save boundary-matchconst boundaryMatch = require('boundary-match');
boundaryMatch('(HotDog)', '(hotdog)', 'i');
// => [ '(HotDog)', index: 0, input: '(HotDog)' ]
// Q. But Why?
// A. Because, these fail to work as expected:
'(HotDog)'.match(/\b\(hotdog\)\b/i);
// => null
'(HotDog)'.match(new RegExp('\\b\\(hotdog\\)\\b', 'i'));
// => null
// When matching isn't enough... `replace`
let matched = boundaryMatch('Gimme a (HotDog)', '(HOTDOG)', 'i');
boundaryMatch.replace(matched, 'Chili Dog!');
// => Gimme a Chili Dog!If the target is found in the string, it returns an Array containing the entire matched target as the first element; an index property for the start of the match; and an input property that contains the entire string. If there were no matches, null is returned.
-
Required:Stringthe string to be searched.
-
Required:Stringthe string to be found. Will be escaped usingescape-string-regexp.
-
Optional:Stringcan have any combination of the following values:i: ignore case
Returns a String that replaces the found match results with target
-
Required:Arraythe result of previously callingboundaryMatch
-
Required:Stringthe string that replaces the found match
- [] Implement other native
RegExpflags:g,u,m,y
ISC © Buster Collings