Versions Compared

Key

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

In this Page

...

Description

Returns a new string with some or all matches of a pattern replaced by another.   The replacement can be a string or a function that is called for each match.

If the first argument is a regular expression, the method will replace all matches if the global flag is set (e.g. /test/g).  If the global flag is not set or the first argument is a string, only the first match will be replaced.

If the replacement is a function, its result will be used as the replacement for a given match.  The function will be passed the following parameters:

  • match: The substring that was matched by the regular expression.
  • p1, p2, ...: The substrings that were captured by the regular expression.
  • offset: The offset of this match within the original string.
  • string: The original string being replaced.

This is similar to the JavaScript replace.

Syntax


Code Block
string.replace(find,replaceWith)


Example


Expression: $msg.replace ("l","x")

where $msg contains "Hello, World"

Result: Hexlo, World


To ignore case, use the /i flag:

Expression: $msg.replace (/world/i,"my friend")

where $msg contains "Hello, World"

Result: Hello, my friend


To globally replace all instances of the letter "l", use the /g flag:

Expression: $msg.replace (/l/g,"x")

where $msg contains "Hello World"

Result: Hexxo Worxd


Expression: $inputstring.replace (/\//g,"_")

where $inputstring contains "/org/projectspace/project"

Result:  _org_projectspace_project

To pass a callback function for matching strings in the regex A-Z

Expression: $value.replace (/ABC/g,"123")

where $value contains "/org/projectspace/project"

Result:  _org_projectspace_project


To capitalize the words in a string:

Expression: $msg.replace(/\b([a-z])(\w+)\b/g, (_matched, first, rest) => first.toUpperCase() + rest.toLowerCase())

where $msg contains "hello, world!"

Result: Hello, World!


...

Description

Returns a new string with text extracted from another string.

If beginIndex is greater than or equal to the length of the string, an empty string is returned. If it is negative, it returns that many characters from the end of the string length.

If endIndex is omitted, slice() extracts to the end of the string. If it is a positive value, the string value is captured up to, but not including that character. If the value is negative, the capture extracts to that number of characters from the end.

This is similar to the JavaScript slice.

Syntax


Code Block
string.slice(beginIndex[, endIndex])

Example

Where $String contains "Copyright 2017 All rights reserved."

Expression: $String.slice(10)

Result: 2017 All rights reserved.


Expression: $String.slice(10,14)

Result: 2017


Expression: $String.slice(10,-2)

Result: 2017 All rights reserve


Expression: $String.slice(-2)

Result: d.


...