replace: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
(Example of replacing a literal string that has regex codes in it.)
Line 21: Line 21:
     This-is-a-test
     This-is-a-test
     This-is-a test
     This-is-a test
<source lang="mtmacro" line>
    [r: tString = "is (a) t"]
    [r: replace("This is (a) test", "\\Q" + tString + "\\E", "-")]
</source>
Returns (This is useful for search+replace of any string you feed to replace() that might have regex codes in it like parenthesis)
    This-est
}}
}}
[[Category:String Function]]
[[Category:String Function]]

Revision as of 19:40, 22 September 2011

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

    [r: replace("This is a test", " ", "-")]
    [r: replace("This is a test", " ", "-", 2)]

Returns

   This-is-a-test
   This-is-a test
    [r: tString = "is (a) t"]
    [r: replace("This is (a) test", "\\Q" + tString + "\\E", "-")]

Returns (This is useful for search+replace of any string you feed to replace() that might have regex codes in it like parenthesis)

This-est