The text transformation definition object. It describes what should be replaced with what.
The input value (from
) can be passed either as a string or as a regular expression.
- If a string is passed, it will be simply checked if the end of the input matches it.
- If a regular expression is passed, its entire length must be covered with capturing groups (e.g.
/(foo)(bar)$/
).
Also, since it is compared against the end of the input, it has to end with $
to be correctly matched.
See examples below.
The output value (to
) can be passed as a string, as an array or as a function.
- If a string is passed, it will be used as a replacement value as-is. Note that a string output value can be used only if
the input value is a string, too.
- If an array is passed, it has to have the same number of elements as there are capturing groups in the input value regular expression.
Each capture group will be replaced with a corresponding string from the passed array. If a given capturing group should not be replaced,
use
null
instead of passing a string.
- If a function is used, it should return an array as described above. The function is passed one parameter — an array with matches
by the regular expression. See the examples below.
A simple string-to-string replacement:
{ from: '(c)', to: '©' }
Change quote styles using a regular expression. Note how all the parts are in separate capturing groups and the space at the beginning
and the text inside quotes are not replaced (null
passed as the first and the third value in the to
parameter):
{
from: /(^|\s)(")([^"]*)(")$/,
to: [ null, '“', null, '”' ]
}
Automatic uppercase after a dot using a callback:
{
from: /(\. )([a-z])$/,
to: matches => [ null, matches[ 1 ].toUpperCase() ]
}