Find / replace

I want to replace one word for another. For instance:

set var0 to “http://example.com/page1.html
set find to “page1”
set replace to “page2”
set result to

I could find in some places some solutions that do not work for me. For instance:
set result to replace_charts(var0, “find”, “replace”)
What am I missing?

A simple replace using text item delimiters.


set var0 to "http://example.com/page1.html"
set findText to "page1"
set replaceText to "page2"
set text item delimiters to findText
set temp to text items of var0
set text item delimiters to replaceText
set resultVar to temp as text

It is not a good idea to name variables such as “find”, “replace”, or “result”
these are or can be verbs and/or reserved words in AppleScript.

It works well, Thank you. But I do not understand the last two lines. I am a beginner. Can you explain, please?

Applescript has a built-in variable called “text item delimiters”
it uses this to parse text into a list of text items
The line

      "set temp to text items of var0"

will parse the contents of var0 into a list into the variable temp - {“http://example.com/”, “.html”}
notice the two items are text that was on either side of “page1”

By setting “text item delimiters” to the replaceText variable we can change the list back to text using the command below

      "set resultVar to temp as text"

This will convert the list of text items in temp back into text with the resultant text being delimited by the new contents of “text item delimiters” - i.e. “http://example.com/page2.html

Thank you