replace()

Replace targeted substrings with new strings.

This function is valid in v2.0.0 to v2.24.3. This function has been downloaded 61 times.

Parameters

{string} subject
String to be modified and returned.
{Array|Object|string} target
If this is a string each instance of this string will be found within subject and replaced by the result of replacer. If this is an array each of the contents will be searched for within subject and replaced by the result of replacer. If this is an object each key will be searched for and replaced by the result of the corresponding value. If the corresponding value of an object's property is a function it will be executed the same way replacer would be.
{function(Array,number,Function)|string} replacer
If a function is supplied it will be called for each target found with argument 1 being the match array (including match.input, match.index, and match.source), argument 2 being the iteration count and argument 3 being a function that when executed will immediately stop replacing values. If a string is supplied it will replace all instances of target within subject. This parameter can be omitted and will be ignored if target is an object.

Returns

{string}
subject with all of the targeted substrings replaced with new strings as specified either by target or replacer.

Required Functions

This function directly requires the following functions which are included automatically:

  • matchAll()
    Gets all of the matches found when matching a regular expression against a string.
  • quoteRegExp()
    Turn any string into a regular expression that matches the exact string without worrying about encoding the metacharacters yourself.
  • typeOf()
    Either gets the type of a value or adds a constructor registering its custom type name.