Here is the documentation page for String.js.
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
/*ignore case*/ "I like cookies".test("COOKIE", "i")
/*returns false*/ "I like cookies".test("cake");
/*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?)");
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
'a bc'.contains('bc'); //true
'a bc'.contains('b', ' '); //false
Returns string with escaped regular expression characters:
'animals.sheeps[1]'.escapeRegExp(); //'animals\.sheeps\[1\]'
Parses the string to an integer.
"10".toInt(); // value is 10
"10px".toInt(); // value is 10
"-10".toInt(); // value is -10
"px10".toInt(); // NaN
"10.1".toInt(); // returns 10
Parses a string to a float (a number with decimal values).
"10".toFloat(); // value is 10
"10.01".toFloat(); // value is 10.01
"-10.01".toFloat(); // value is -10.01
Converts a hyphenated string into a mixed case string; mostly used by MooTools for setting and getting css properties.
"I-like-cookies".camelCase(); //"ILikeCookies"
Converts a CamelCase string to a hyphenated one; mostly used by MooTools for setting and getting css properties.
"ILikeCookies".hyphenate(); //"I-like-cookies"
"i like cookies".capitalize(); //"I Like Cookies"
" i like cookies ".trim() //"i like cookies"
Trims (String.trim) a string AND removes all the double spaces in a string.
" i like cookies \n\n".clean() //"i like cookies"
"rgb(17,34,51)".rgbToHex(); //"#112233"
"rgba(17,34,51,0)".rgbToHex(); //"transparent"
"rgb(17,34,51)".rgbToHex(true); //[11,22,33]
"#112233".hexToRgb(); //"rgb(17,34,51)"
"#112233".hexToRgb(true); //[17,34,51]
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."*/
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