A collection of the String Object prototype methods.
See Also:
Searches for a match between the string and a regular expression. For more information see MDC Regexp:test.
Syntax:
myString.test(regex[,params]);
Arguments:
- regex - (mixed) The string or regular expression you want to match the string with.
- params - (string, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).
Returns:
- (boolean)
true
, if a match for the regular expression is found in this string. - (boolean)
false
if is not found
Examples:
"I like cookies".test("cookie"); //returns true
"I like cookies".test("COOKIE", "i"); //returns true (ignore case)
"I like cookies".test("cake"); //returns false
See Also:
Checks to see if the string passed in is contained in this string. If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.
Syntax:
myString.contains(string[, separator]);
Arguments:
- string - (string) The string to search for.
- separator - (string, optional) The string that separates the values in this string (eg. Element classNames are separated by a ' ').
Returns:
- (boolean)
true
if the string is contained in this string - (boolean)
false
if not.
Examples:
'a bc'.contains('bc'); //returns true
'a b c'.contains('c', ' '); //returns true
'a bc'.contains('b', ' '); //returns false
Trims the leading and trailing spaces off a string.
Syntax:
myString.trim();
Returns:
- (string) The trimmed string.
Examples:
" i like cookies ".trim(); //"i like cookies"
Removes all extraneous whitespace from a string and trims it (String:trim).
Syntax:
myString.clean();
Returns:
- (string) The cleaned string.
Examples:
" i like cookies \n\n".clean(); //returns "i like cookies"
Converts a hyphenated string to a camelcased string.
Syntax:
myString.camelCase();
Returns:
- (string) The camelcased string.
Examples:
"I-like-cookies".camelCase(); //returns "ILikeCookies"
Converts a camelcased string to a hyphenated string.
Syntax:
myString.hyphenate();
Returns:
- (string) The hyphenated string.
Examples:
"ILikeCookies".hyphenate(); //returns "I-like-cookies"
Converts the first letter of each word in a string to uppercase.
Syntax:
myString.capitalize();
Returns:
- (string) The capitalized string.
Examples:
"i like cookies".capitalize(); //returns "I Like Cookies"
Escapes all regular expression characters from the string.
Syntax:
myString.escapeRegExp();
Returns:
- (string) The escaped string.
Examples:
'animals.sheep[1]'.escapeRegExp(); //returns 'animals\.sheep\[1\]'
Parses this string and returns a number of the specified radix or base.
Syntax:
myString.toInt([base]);
Arguments:
- base - (number, optional) The base to use (defaults to 10).
Returns:
- (number) The number.
- (NaN) If the string is not numeric, returns NaN.
Examples:
"4em".toInt(); //returns 4
"10px".toInt(); //returns 10
See Also:
Parses this string and returns a floating point number.
Syntax:
myString.toFloat();
Returns:
- (number) The float.
- (NaN) If the string is not numeric, returns NaN.
Examples:
"95.25%".toFloat(); //returns 95.25
"10.848".toFloat(); //returns 10.848
See Also:
Converts a hexidecimal color value to RGB. Input string must be in one of the following hexidecimal color formats (with or without the hash). '#ffffff', #fff', 'ffffff', or 'fff'
Syntax:
myString.hexToRgb([array]);
Arguments:
- array - (boolean, optional) If true is passed, will output an array (eg. [255, 51, 0]) instead of a string (eg. "rgb(255,51,0)").
Returns:
- (string) A string representing the color in RGB.
- (array) If the array flag is set, an array will be returned instead.
Examples:
"#123".hexToRgb(); //returns "rgb(17,34,51)"
"112233".hexToRgb(); //returns "rgb(17,34,51)"
"#112233".hexToRgb(true); //returns [17, 34, 51]
Converts an RGB color value to hexidecimal. Input string must be in one of the following RGB color formats. "rgb(255,255,255)", or "rgba(255,255,255,1)"
Syntax:
myString.rgbToHex([array]);
Arguments:
- array - (boolean, optional) If true is passed, will output an array (eg. ['ff','33','00']) instead of a string (eg. "#ff3300").
Returns:
- (string) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.
- (array) If the array flag is set, an array will be returned instead.
Examples:
"rgb(17,34,51)".rgbToHex(); //returns "#112233"
"rgb(17,34,51)".rgbToHex(true); //returns ['11','22','33']
"rgba(17,34,51,0)".rgbToHex(); //returns "transparent"
See Also:
Strips the String of its <script>
tags and anything in between them.
Syntax:
myString.stripScripts([evaluate]);
Arguments:
- evaluate - (boolean, optional) If true is passed, the scripts within the String will be evaluated.
Returns:
- (string) - The String without the stripped scripts.
Examples:
var myString = "<script>alert('Hello')</script>Hello, World.";
myString.stripScripts(); //Returns "Hello, World."
myString.stripScripts(true); //Alerts "Hello", then returns "Hello, World."
Substitutes keywords in a string using an object/array. Removes undefined keywords and ignores escaped keywords.
Syntax:
myString.substitute(object[, regexp]);
Arguments:
- object - (mixed) The key/value pairs used to substitute a string.
- regexp - (regexp, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .
Returns:
- (string) - The substituted string.
Examples:
var myString = "{subject} is {property_1} and {property_2}.";
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'};
myString.substitute(myObject); //Jack Bauer is our lord and savior