JavaScript

Language Extensions
Array.from( arrayLikeObject ) static
Number.prototype.toInt()
Number.prototype.toLong()
Number.prototype.toFloat()
String.prototype.trim()
String.prototype.trimLeft()
String.prototype.trimRight()
String.prototype.replaceAll( pattern, replacement )
String.prototype.startsWith( pattern )
String.prototype.endsWith( pattern )
RegExp.quote( string ) static
RegExp.quoteReplacement( string ) static

Language Extensions

This document describes extensions to the standard JavaScript language. These are defined in the common library, but grouped together here for convenience. There is no need to import this file.

Array.from( arrayLikeObject ) static

Create a JavaScript Array object from any "array-like" object, including a Java array. An array-like object a includes a length property and a sequence of zero or more sequential properties a[0], a[1], a[2], ... a[ a.length-1 ].

returns an Array containing a copy of the elements of arrayLikeObject

Number.prototype.toInt()

Converts this number to a Java Integer object. Any fractional part is discarded. If the number lies outside the range of possible Integer values, an exception is thrown.

returns this number as a java.lang.Integer

Number.prototype.toLong()

Converts this number to a Java Long object. Any fractional part is discarded. If the number lies outside the range of possible Long values, an exception is thrown.

returns this number as a java.lang.Long

Number.prototype.toFloat()

Converts this number to a Java Float object. If the number lies outside the range of possible Float values, an exception is thrown.

returns this number as a java.lang.Float

String.prototype.trim()

Removes leading and trailing whitespace from this string.

returns this string with whitespace removed from both ends

String.prototype.trimLeft()

Removes leading whitespace from this string.

returns this string with whitespace removed from the start

String.prototype.trimRight()

Removes trailing whitespace from this string.

returns this string with whitespace removed from the end

String.prototype.replaceAll( pattern, replacement )

Replaces every occurrence of pattern with replacement, where both values are plain strings rather than regular expressions. The replacements are made as if by processing the string from left to right and replacing each time a match is possible. For example, the following code results in bb rather than bbb:
 println( "aaaa".replaceAll( "aa", "b" ) );

pattern the substring to replace all occurrences of
replacement the substring to replace each occurrence with

returns the replaced string

String.prototype.startsWith( pattern )

Returns true if pattern is a prefix of this string.
 "meta".startsWith( "me" ); // true
 "Random".startsWith( "and" ); // false

pattern the string to match against the start of this string

returns true if and only if this string starts with pattern

String.prototype.endsWith( pattern )

Returns true if pattern is a suffix of this string.
 "bartend".endsWith( "tend" ); // true
 "moose".endsWith( "oos" ); // false

pattern the string to match against the end of this string

returns true if and only if this string ends with pattern

RegExp.quote( string ) static

Escapes the characters in a string that have special meaning in a regular expression. If a regular expression is created from the string, it will match string literally. For example, * would be replaced with \* because * has special meaning in a regular expression.

string the string to escape

returns a string that encodes string as regular expression

RegExp.quoteReplacement( string ) static

Escapes the characters in a string that have special meaning when used as a replacement for a regular expression.

string the string to escape
returns a string that encodes string as regular expression replacement

Index    Contents