You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns an object which has three functions, with the keys `interpolate`, `interpolateSlash`, and `interpolateDollar`.
23
+
24
+
These three functions each accept two arguments: a match array and a replacement string.
25
+
26
+
The match array corresponds to the arguments passed into the replacement function in `String.prototype.replace()`, that is, it should have the following values:
27
+
28
+
- Matched substring
29
+
- First capture
30
+
- Second capture
31
+
- ...
32
+
- The offset of the matched substring within the whole string being examined.
33
+
- The whole string being examined
34
+
35
+
Thus, the array **should** have at least three arguments, as captures being optional, their number being determined by the regular expression pattern.
36
+
37
+
### `interpolateDollar`
38
+
39
+
This function is intended to correspond exactly to JavaScript's own replacement behaviour, i.e., the following should have the same result:
$` The portion of the string that precedes the matched substring.
54
+
$' The portion of the string that follows the matched substring.
55
+
$1 The contents of the first captured substring.
56
+
$2 The contents of the second captured substring.
57
+
...
58
+
$$ A literal dollar character
59
+
```
60
+
61
+
Note that `$1`, `$2`... `$n`. are only interpolated if the pattern has at least _n_ captures.
62
+
63
+
### `interpolateSlash`
64
+
65
+
This function uses backslash as an escape character, which is useful for situations where you are replacing from user input.
66
+
67
+
It perfoms the following interpolations, corresponding broadly to JavaScript's String Literal syntax:
68
+
69
+
```
70
+
\1 The contents of the first captured substring.
71
+
\2 The contents of the second captured substring.
72
+
...
73
+
\\ A literal backslash character.
74
+
\0 A literal NUL (0x00) character.
75
+
\n A newline.
76
+
\r A carriage return.
77
+
\t A tab character.
78
+
\v A vertical tab character.
79
+
\$ A literal dollar character (this works for any non-word character).
80
+
\xhh The codepoint at 0xhh.
81
+
\uhhhh The codepoint at 0xhh.
82
+
\u{h}, \u{hhhh}, \x{hhh}, etc.
83
+
```
84
+
85
+
Note that `\1`, `\2`... `\n`. are only interpolated if the pattern has at least _n_ captures.
86
+
87
+
Note also that if writing the replacement in JavaScript source, your backslashes may need escaping, i.e. the following are equivalent:
88
+
89
+
```js
90
+
Interpolation.interpolateSlash(match, "\\1");
91
+
Interpolation.interpolateDollar(match, "$1");
92
+
```
93
+
94
+
### `interpolate`
95
+
96
+
This function interpolates both slash-escaped and dollar-escaped tokens.
97
+
98
+
This function is designed to be used in a user-facing environment where DWIM (do what I mean) is an important principle, i.e. the user might expect to use dollar or slash syntax, or even both.
99
+
100
+
# Author & Contributors
101
+
102
+
Daniel Perrett <perrettdl@googlemail.com>
103
+
104
+
# Copyright & License
105
+
106
+
This package is available under a MIT License - see the included `LICENSE` file for more information.
0 commit comments