String
strReplace

Returns a new string with one, some, or all matches of a pattern replaced by a replacement.

Syntax

import { strReplace } from '@opentf/std';
 
strReplace(str: string,
           pattern: string | RegExp,
           replacement: string | Function,
           options?: {all: boolean, case: boolean}
): string;

The replacement function can be in the form of specifying_a_function_as_the_replacement (opens in a new tab)

  • The option all refers to RegExp g global flag.
  • The option case refers to RegExp i ignore case / case insensitive flag.

Examples

strReplace('abc', 'a', 'x') //=> 'xbc'
 
strReplace('abbc', 'b', '', { all: true }) //=> 'ac'
 
strReplace('aBbBc', 'B', '', { all: true, case: true }) //=> 'ac'
 
const paragraph = "I think Ruth's dog is cuter than your dog!";
const regex = /dog/;
strReplace(paragraph, regex, 'ferret') //=> "I think Ruth's ferret is cuter than your dog!"
 
const str = 'Twas the night before Xmas...';
strReplace(str, /xmas/, 'Christmas', { case: true }) //=> 'Twas the night before Christmas...'
 
const str = 'Apples are round, and apples are juicy.';
strReplace(str, /apple/, 'Orange', { all: true, case: true }) //=> 'Oranges are round, and Oranges are juicy.'
 
function convert(str, p1) {
  return `${((p1 - 32) * 5) / 9}C`;
}
const test = /(-?\d+(?:\.\d*)?)F\b/;
strReplace('212F', test, convert) //=> '100C'

Try