Native: String

A collection of the String Object prototype methods.

Смотрите также:

String Method: test

Searches for a match between the string and a regular expression. For more information see MDC Regexp:test.

Синтаксис:

myString.test(regex[,params]);

Аргументы:

  1. regex - (mixed) The string or regular expression you want to match the string with.
  2. params - (string, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).

Возвращает:

  • (boolean) true, if a match for the regular expression is found in this string.
  • (boolean) false if is not found

Примеры:

"I like cookies".test("cookie"); //returns true
"I like cookies".test("COOKIE", "i"); //returns true (ignore case)

"I like cookies".test("cake"); //returns false

Смотрите также:

String Method: contains

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.

Синтаксис:

myString.contains(string[, separator]);

Аргументы:

  1. string - (string) The string to search for.
  2. separator - (string, optional) The string that separates the values in this string (eg. Element classNames are separated by a ' ').

Возвращает:

  • (boolean) true if the string is contained in this string
  • (boolean) false if not.

Примеры:

'a bc'.contains('bc'); //returns true
'a b c'.contains('c', ' '); //returns true

'a bc'.contains('b', ' '); //returns false

String Method: trim

Trims the leading and trailing spaces off a string.

Синтаксис:

myString.trim();

Возвращает:

  • (string) The trimmed string.

Примеры:

"    i like cookies     ".trim(); //"i like cookies"

String Method: clean

Removes all extraneous whitespace from a string and trims it (String:trim).

Синтаксис:

myString.clean();

Возвращает:

  • (string) The cleaned string.

Примеры:

" i      like     cookies      \n\n".clean(); //returns "i like cookies"

String Method: camelCase

Converts a hyphenated string to a camelcased string.

Синтаксис:

myString.camelCase();

Возвращает:

  • (string) The camelcased string.

Примеры:

"I-like-cookies".camelCase(); //returns "ILikeCookies"

String Method: hyphenate

Converts a camelcased string to a hyphenated string.

Синтаксис:

myString.hyphenate();

Возвращает:

  • (string) The hyphenated string.

Примеры:

"ILikeCookies".hyphenate(); //returns "I-like-cookies"

String Method: capitalize

Converts the first letter of each word in a string to uppercase.

Синтаксис:

myString.capitalize();

Возвращает:

  • (string) The capitalized string.

Примеры:

"i like cookies".capitalize(); //returns "I Like Cookies"

String Method: escapeRegExp

Escapes all regular expression characters from the string.

Синтаксис:

myString.escapeRegExp();

Возвращает:

  • (string) The escaped string.

Примеры:

'animals.sheep[1]'.escapeRegExp(); //returns 'animals\.sheep\[1\]'

String Method: toInt

Parses this string and returns a number of the specified radix or base.

Синтаксис:

myString.toInt([base]);

Аргументы:

  1. base - (number, optional) The base to use (defaults to 10).

Возвращает:

  • (number) The number.
  • (NaN) If the string is not numeric, returns NaN.

Примеры:

"4em".toInt(); //returns 4
"10px".toInt(); //returns 10

Смотрите также:

String Method: toFloat

Parses this string and returns a floating point number.

Синтаксис:

myString.toFloat();

Возвращает:

  • (number) The float.
  • (NaN) If the string is not numeric, returns NaN.

Примеры:

    "95.25%".toFloat(); //returns 95.25
    "10.848".toFloat(); //returns 10.848

Смотрите также:

String Method: hexToRgb

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'

Синтаксис:

myString.hexToRgb([array]);

Аргументы:

  1. array - (boolean, optional) If true is passed, will output an array (eg. [255, 51, 0]) instead of a string (eg. "rgb(255,51,0)").

Возвращает:

  • (string) A string representing the color in RGB.
  • (array) If the array flag is set, an array will be returned instead.

Примеры:

"#123".hexToRgb(); //returns "rgb(17,34,51)"
"112233".hexToRgb(); //returns "rgb(17,34,51)"
"#112233".hexToRgb(true); //returns [17, 34, 51]

String Method: rgbToHex

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)"

Синтаксис:

myString.rgbToHex([array]);

Аргументы:

  1. array - (boolean, optional) If true is passed, will output an array (eg. ['ff','33','00']) instead of a string (eg. "#ff3300").

Возвращает:

  • (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.

Примеры:

"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"

Смотрите также:

String Method: stripScripts

Strips the String of its <script> tags and anything in between them.

Синтаксис:

myString.stripScripts([evaluate]);

Аргументы:

  1. evaluate - (boolean, optional) If true is passed, the scripts within the String will be evaluated.

Возвращает:

  • (string) - The String without the stripped scripts.

Примеры:

var myString = "<script>alert('Hello')</script>Hello, World.";
myString.stripScripts(); //Returns "Hello, World."

myString.stripScripts(true); //Alerts "Hello", then returns "Hello, World."

String Method: substitute

Substitutes keywords in a string using an object/array. Removes undefined keywords and ignores escaped keywords.

Синтаксис:

myString.substitute(object[, regexp]);

Аргументы:

  1. object - (mixed) The key/value pairs used to substitute a string.
  2. regexp - (regexp, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .

Возвращает:

  • (string) - The substituted string.

Примеры:

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


Эта документация распостраняется на правах Attribution-NonCommercial-ShareAlike 3.0 License.
Оригинал документации на английском.
© Linux.ria.ua, 2008-2024