The Parameters
Can be a string or an object with a replacement Can be a string or a function.
Return valueA new string, with one, some, or all matches of the pattern replaced by the specified replacement. DescriptionThis method does not mutate the string value it's called on. It returns a new string. A string pattern will only be replaced once. To perform a global search and replace, use a
regular expression with the If If the
A regexp
with the Specifying a string as the replacementThe replacement string can include the following special replacement patterns:
Specifying a function as the replacementYou can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. Note: The above-mentioned special replacement patterns do not apply for strings returned from the replacer function. The function has the following signature:
The arguments to the function are as follows: match The matched substring. (Corresponds to p1, p2, …, pN
The offset The offset of the matched substring within the whole string being examined. For example, if the whole string was string The whole string being examined. groups An object whose keys are the used group
names, and whose values are the matched portions ( The exact number of arguments depends on whether the first argument is a The following example will set
The function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global. ExamplesDefining the regular expression in replace()In the following example, the regular expression is defined in
This logs Using the global and ignoreCase flags with replace()Global replace can only be done with a regular expression. In the following example, the regular expression includes the
global and ignore case flags which permits
This logs Switching words in a stringThe following script switches the words in the string. For the replacement text, the script uses capturing groups and the
This logs Using an inline function that modifies the matched charactersIn this example, all occurrences of capital letters in the string are converted to lower case, and a hyphen is inserted just before the match location. The important thing here is that additional operations are needed on the matched item before it is given back as a replacement. The replacement function accepts the matched snippet as its parameter, and uses it to transform the case and concatenate the hyphen before returning.
Given Because we
want to further transform the result of the match before the final substitution is made, we must use a function. This forces the evaluation of the match prior to the
This is because Replacing a Fahrenheit degree with its Celsius equivalentThe following example replaces a Fahrenheit degree with its equivalent Celsius degree. The Fahrenheit degree should be a number ending with The regular expression
Making a generic replacerSuppose we want to create a replacer that appends the offset data to every matched string. Because the replacer
function already receives the
However, this replacer would be hard to generalize if we want it to work with any regex pattern. The replacer is variadic — the number of arguments it receives depends on the number of capturing groups present. We can use rest parameters, but
it would also collect
The Instead, you need to extract the last few arguments based on type, because
Specifications
Browser compatibilityBCD tables only load in the browser See alsoWhat inserts new text within existing text?Insert mode
The method of operation used for inserting new text within existing text in a document. Insert mode is the default.
Which method can you use to move text from one part of the document to another quizlet?cut and paste. to move text from location to another in a document.. cut. to remove text from the document.. Paste. Insert text that is stored on the Clipboard in the document at the location of the insertion point.. Clipboard. ... . drag and drop. ... . Save. ... . Save As. ... . system Clipboard.. Which of the following is a set of coordinated colors fonts and effects which can be applied to a document?Word Ch 1 Lessons 1&2. What is an insertion point in word?The location at which text or a node is inserted. The insertion point for text appears as a flashing vertical bar. As you type text, the flashing vertical bar moves to the right of the text you type.
|