replace: Difference between revisions
Jump to navigation
Jump to search
m (Tweaking page layout and typography) |
No edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
replace(str, pattern, value) | replace(str, pattern, value) | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
replace(str, pattern, value, times) | replace(str, pattern, value, times) | ||
</ | </syntaxhighlight> | ||
|example= | |example= | ||
'''Example 1''' | '''Example 1''' | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: replace("This is a test", " ", "-")] | [r: replace("This is a test", " ", "-")] | ||
[r: replace("This is a test", " ", "-", 2)] | [r: replace("This is a test", " ", "-", 2)] | ||
</ | </syntaxhighlight> | ||
Returns: | Returns: | ||
This-is-a-test | This-is-a-test | ||
Line 26: | Line 26: | ||
'''Example 2'''<br> | '''Example 2'''<br> | ||
This is useful for search+replace of any string you feed to {{code|replace()}} that might have regex codes in it like parenthesis: | This is useful for search+replace of any string you feed to {{code|replace()}} that might have regex codes in it like parenthesis: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: tString = "is (a) t"] | [r: tString = "is (a) t"] | ||
[r: replace("This is (a) test", "\\Q" + tString + "\\E", "-")] | [r: replace("This is (a) test", "\\Q" + tString + "\\E", "-")] | ||
</ | </syntaxhighlight> | ||
Returns: | Returns: | ||
This-est | This-est | ||
'''Example 3''' | '''Example 3''' | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: name = replace("wolph 5","(.*?) [0-9]+","\$1 42")] | [r: name = replace("wolph 5","(.*?) [0-9]+","\$1 42")] | ||
</ | </syntaxhighlight> | ||
returns: | returns: | ||
wolph 42 | wolph 42 | ||
Line 43: | Line 43: | ||
'''Example 4'''<br> | '''Example 4'''<br> | ||
Note that multiple {{code|.*}} in the search string correspond with multiple {{code|$#}}: | Note that multiple {{code|.*}} in the search string correspond with multiple {{code|$#}}: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h:lastPath = [{"x":1,"y":0},{"x":1,"y":1},{"x":1,"y":2}]] | [h:lastPath = [{"x":1,"y":0},{"x":1,"y":1},{"x":1,"y":2}]] | ||
[r:result = replace(lastPath, '\\{"x":(.*?),"y":(.*?)\\}', '"X\$1Y\$2"')] | [r:result = replace(lastPath, '\\{"x":(.*?),"y":(.*?)\\}', '"X\$1Y\$2"')] | ||
</ | </syntaxhighlight> | ||
returns: | returns: | ||
["X1Y0","X1Y1","X1Y2"] | ["X1Y0","X1Y1","X1Y2"] |
Latest revision as of 23:59, 14 March 2023
replace() Function
• Introduced in version 1.3b48
Returns the string with the occurrences of a pattern replaced by the specified value. If the number of times to perform the replacement is not specified then all occurrences of the pattern are replaced. Pattern can be a regular expression. This means if the pattern string contains any regular expression special characters they must be escaped.
Usage
replace(str, pattern, value)
replace(str, pattern, value, times)
Example
Example 1
[r: replace("This is a test", " ", "-")]
[r: replace("This is a test", " ", "-", 2)]
Returns:
This-is-a-test This-is-a test
Example 2
This is useful for search+replace of any string you feed to replace()
that might have regex codes in it like parenthesis:
[r: tString = "is (a) t"]
[r: replace("This is (a) test", "\\Q" + tString + "\\E", "-")]
Returns:
This-est
Example 3
[r: name = replace("wolph 5","(.*?) [0-9]+","\$1 42")]
returns:
wolph 42
The $
normally means to match end-of-line, but only when it's at the end of the pattern. In this case, it's at the beginning of the pattern. And that means if the character immediately following is a digit (such as $1
), those characters are replaced by the source string matched by the corresponding set of parentheses in the regular expression.
Example 4
Note that multiple .*
in the search string correspond with multiple $#
:
[h:lastPath = [{"x":1,"y":0},{"x":1,"y":1},{"x":1,"y":2}]]
[r:result = replace(lastPath, '\\{"x":(.*?),"y":(.*?)\\}', '"X\$1Y\$2"')]
returns:
["X1Y0","X1Y1","X1Y2"]