Table of Contents

Strings

Here is the documentation page for String.js.

String.test

Tests a string against a regular expression; optional second parameter for Regex options. Accepts regular expressions in both string and regexp mode.

"I like cookies".test("cookie"); // returns true
execute this code
/*ignore case*/
"I like cookies".test("COOKIE", "i")
execute this code
/*returns false*/
"I like cookies".test("cake");
execute this code
/*here's an example of how you would probably actually use it*/
if("I like cookies".test("cake")) alert ('you like cake');
else alert("you don't like cake (what's wrong with you?)");
execute this code

String.contains

Checks if the passed in string is contained in the string. Also accepts an optional second parameter, to check if the string is contained in a list of separated values. The big difference between this and String.test is that String.contains does not use regular expressions. The second argument also is notable, as it basically performs a String.split on the string and then searches the results:

'a b c'.contains('c', ' '); //true
execute this code
'a bc'.contains('bc'); //true
execute this code
'a bc'.contains('b', ' '); //false
execute this code

String.escapeRegExp

Returns string with escaped regular expression characters:

'animals.sheeps[1]'.escapeRegExp(); //'animals\.sheeps\[1\]'
execute this code

String.toInt

Parses the string to an integer.

"10".toInt(); // value is 10
execute this code
"10px".toInt(); // value is 10
execute this code
"-10".toInt(); // value is -10
execute this code
"px10".toInt(); // NaN
execute this code
"10.1".toInt(); // returns 10
execute this code

String.toFloat

Parses a string to a float (a number with decimal values).

"10".toFloat(); // value is 10
execute this code
"10.01".toFloat(); // value is 10.01
execute this code
"-10.01".toFloat(); // value is -10.01
execute this code

String.camelCase

Converts a hyphenated string into a mixed case string; mostly used by MooTools for setting and getting css properties.

"I-like-cookies".camelCase(); //"ILikeCookies"
execute this code

String.hyphenate

Converts a CamelCase string to a hyphenated one; mostly used by MooTools for setting and getting css properties.

"ILikeCookies".hyphenate(); //"I-like-cookies"
execute this code

String.capitalize

"i like cookies".capitalize(); //"I Like Cookies"
execute this code

String.trim

"    i like cookies     ".trim() //"i like cookies"
execute this code

String.clean

Trims (String.trim) a string AND removes all the double spaces in a string.

" i      like     cookies      \n\n".clean() //"i like cookies"
execute this code

String.rgbToHex

"rgb(17,34,51)".rgbToHex(); //"#112233"
execute this code
"rgba(17,34,51,0)".rgbToHex(); //"transparent"
execute this code
"rgb(17,34,51)".rgbToHex(true); //[11,22,33]
execute this code

String.hexToRgb

"#112233".hexToRgb(); //"rgb(17,34,51)"
execute this code
"#112233".hexToRgb(true); //[17,34,51]
execute this code

String.stripScripts

This method removes all the <script> tags from a string. If you pass in true as an argument, those scripts will also be evaluated.

var myString = "<script>alert('Hello')</script>Hello, World.";
console.log(myString.stripScripts()); /*Returns "Hello, World."*/
console.log(myString.stripScripts(true)); /*Alerts "Hello", then returns "Hello, World."*/
execute this code

String.substitute

This method will subsitute the values in an object for the keys in a string. Each key must be wrapped with squigly brackets (you can change this if you want to use something else - see the docs).

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
execute this code