Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Doc new sprintf escaping

...

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:

...

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 and \\ to escape a literal \:

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 from the end of the string. For example:

...