' Ruby-style
user = "PJ"
return "Welcome back, #{user}!"
' ES5/ES6 style
user = "PJ"
return `Welcome back, ${user}!`
Return "Welcome back, " + user + "!"
thing = "book"
color = "red"
print Substitute("My {0} is {1}.", thing, color)
' prints "My book is red."
thing = "book"
color = "red"
print "My %s is %s.".Format(thing, color)
' prints "My book is red."
thing = "book"
color = "red"
print "My ${thing} is ${color}.".Replace("${thing}", thing).Replace("${color}", color)
' prints "My book is red."
' input = "foo ${bar} baz"
' subsAA = {bar: "xyzzy"}
' output = "foo xyzzy baz"
'cache macro regex singleton somewhere
macroRegEx = m.macroRegEx
if macroRegEx = invalid
macroRegEx = createObject("roRegEx", "\${([^}]*)}", "")
m.macroRegEx = macroRegEx
end if
matches = macroRegEx.matchAll(input) ' get all macro matches (think "introns" in a gene)
if matches.count() = 0
output = input ' nothing to replace
else
gaps = macroRegEx.split(input) ' all "exons" between them
' general case is (note that re.split() compresses empty matches at end of list)
' input = gap0 macro0 gap1 macro1 ... macroN gapN [macroN+1 ... macroN+K]
gaps.reset() ' reset() needed to iterate over roList
if gaps.isNext() then output = gaps.next() else output = ""
for each match in matches
val = subsAA[match[1]]
if val = invalid then match = match[0] ' not found - leave macro
output += val.toStr()
if gaps.isNext() then output += gaps.next()
end for
end if
thing = "book"
color = "red"
print "My %s is %s.".Format(thing, color)
' prints "My book is red."
This is working answer.