New Velocity tools and methods

Back to What's New

Aug 14, 2020

New Regex Tool

A new Regex Tool allows you to compile regular expressions for comparison and matching use. Example:

$_RegexTool.compile("^[a-zA-Z0-9]*$")

#set ($text = "A string to search for links containing the pattern https:// .")
#set ($patternString = ".*https://.*")
#set ($pattern = $_RegexTool.compile($patternString))
#set ($matcher = $pattern.matcher($text))
#set ($matches = $matcher.matches())
## Expected output: true

New String Tool methods

If you need to create unique IDs for elements such as element id attributes, you can now generate universally unique identifiers with the .generateUUID() method. Example:

$_StringTool.generateUUID()
## Sample output: 29ecf0f3-7db4-41b5-94a2-3e170338c3b5

We've also added a new method to build Strings. Example:

#set ($stringBuilder = $_StringTool.getStringBuilder())
#set ($_void = $stringBuilder.append("foo"))
#set ($_void = $stringBuilder.append("+"))
#set ($_void = $stringBuilder.append("bar"))
$stringBuilder
## Sample output: foo+bar

New Escape Tool methods

We've added several new methods to unescape HTML, JavaScript, and XML markup. Examples:

$_EscapeTool.unescapeHtml('Here is a String with an escaped ampersand (&).')
## Expected output: Here is a String with an escaped ampersand (&).
$_EscapeTool.unescapeJavaScript('if (i === \"foo\")')
## Expected output: if (i === "foo").
$_EscapeTool.unescapeHtml('<foo>bar</foo>')
## Expected output: <foo>bar</foo>

For more information about tools and methods available in Velocity, see our Velocity Tools page.

Categories

Development