replace

Typecommand
DictionaryLCS
LibraryLiveCode Script
Syntax
replace <oldString> with <newString> in <container>
Summary

Replaces text in a container with other text.

Introduced1.0
OSmac, windows, linux, ios, android
Platformsdesktop, server, mobile
Parameters
NameTypeDescription
oldString

Any expression that evaluates to a string, and specifies the text to replace.

newString

Any expression that evaluates to a string, and specifies the text to replace the oldString with.

container

A field, button, or variable, or the message box.

Example
replace "A" with "N" in thisVariable -- changes all As to Ns
replace return with empty in field 1 -- runs lines together
replace somethingOld with "\" in it
replace ".com" with somethingNew in URL "file:stuff.txt"
put "12.34.56.78" into p
replace "." with empty in char 4 to -1 of p  -- 12.345678
RelatedProperty: itemDelimiter
Control Structure: function
Keyword: string, field
Object: field
Glossary: command, regular expression, container, property
Command: find, filter, replace, replace in field
Function: replaceText
Description

Use the replace command to replace all instances of one string with another string.

The replace command is faster than the replaceText function, but does not support regular expressions: you can replace only an exact string of text.

Note: The replace command is not recursive and behaves in much the same way a text editor would perform a plain text search, looking at each character in the text once only. This means that if there are two overlapping instances of matching text, only the first will be replaced. For example:

put "x:y:y:y:y:x:" into tChangeMe
replace ":y:" with "::" in tChangeMe
put tChangeMe -- returns x::y::y:x

If the goal were to remove every lone y within two colons that appears in the text, it would be more effective to take advantage of how the text to remove is contained within two identical characters, along with the itemDelimiter property, such as in the following:

put "x:y:y:y:y:x" into tChangeMe
set the itemDelimiter to ":"
repeat with tCount = 2 to (the number of items in tChangeMe) - 1
    if item tCount of tChangeMe is "y" then 
        put empty into item tCount of tChangeMe
    end if
end repeat
put tChangeMe -- returns x:::::x

Important: You can use the replace command on a field, but doing so removes any formatting (fonts, styles, colors, and sizes) in the field. To preserve existing styling in fields use the replace in field command.

Tagstext processing