Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejavascript
themeConfluence
my_str = "This is a string";
my_url = "http://www.amazon.com/gp/products/123456789/";
a = my_str.replace(re#is#,"ese");                            // a = "These is a string"
b = my_str.replace(re#is#g,"ese");                           // b = "These ese a string"
c = my_str.replace(re#this#,"That");                         // c = "This is a string" (no change)
d = my_str.replace(re#this#i,"That");                        // d = "That is a string"
e = my_url.replace(re#^http://(\[A-Za-z0-9.-\]+)/.*#,"$1");  // e = "www.amazon.com"

Refer to the full table for other replacement symbols.

In the final example, I was careful to ensure that the regular expression matched the entire URL, so that the substitution of $1 resulted in just the domain name. This same result could be more easily achieved using extract()—although the result, e, would be an array:

Code Block
languagejavascript
themeConfluence
e = my_url.extract(re#^http://(\[A-Za-z0-9.-\]+)/#); // e = [ "www.amazon.com" ]

As of version 0.45.5, the second argument to the replace() operator can be a function. When a match is found, this function will be called to determine the replacement string. It will be given, as its arguments, the string that matched, the capture groups in order, the index of the start of the match in the overall string (upon which replace is operating), and finally the overall string itself. For example:

Code Block
"one 1 two 2 three 3".replace(re#([a-z]+) ([0-9])#g, match_function)

Because the regular expression has the global flag (g), the expression will match in three places and the match_function will be called three times. The arguments for each of the three calls are shown here:

Code Block
linenumberstrue
"one 1", "one", "1", 0, "one 1 two 2 three 3"
"two 2", "two", "2", 6, "one 1 two 2 three 3"
"three 3", "three", "3", 12, "one 1 two 2 three 3"

split()

The split() operator takes a regular expression as its sole argument. The regular expression is used to split the original string into an array. For example:

...

The sprintf() operator can be applied to either a number or a string. The operator takes a formatting string as its sole argument. Currently, the formatting string for strings supports %s and does not support escaping  Use \%s to escape a literal %s:

Code Block
languagejavascript
themeConfluence
a = "Hello world!"
b = a.sprintf("< %s ><%s \%s>") // b = "< Hello<Hello world! >%s>"

substr()

The substr() operator returns a substring of the target string. The operator takes an argument that gives an offset from the start of the string to begin returning the substring and an optional length that gives the desired length of the substring. If the length is omitted, the substring to the end of the original string from the offset will be returned. If the length is negative, the length will be caluclated calculated from the end of the string. For example:

...