Strings

var str1 = "double quotes"; 
var ch = str1[2], len = str1.length; // string as characters array
var str2 = 'single quotes';
var str3 = new String("creates via String object"); 
var str4 = "nesting 'quotes' allowed";
var str5 = "my long long\
string";
var str6 = "preferred way for long string " +
                  "is using + operator";
var str7 = "escape \"sequences\" supported";

// string templates (ES6)
var str8 = ` ${any_var_expression} ${2+3}`;
var str9 = ` long string with 
template`;

escape sequences

esc. seq. description
\" Inserts a double quote.
\' Inserts a single quote.
\\ Inserts a backslash.
\n Inserts a new line.
\r Inserts a carriage return.
\v Inserts a vertical tab.
\t Inserts a tab.
\f Form feed.
\uXXXX Inserts a unicode character with code XXXX (4 hex digits). Possible values are in range of 0x0000–0xFFFF.

methods

Index or position of character in string is zero-based.
Parameters of methods in square braces [] are optional and can be omitted.

Methods
method description
indexOf(txt[, from]) Finds first occurrence of text. Parameters:
  • txt - text for searching
  • from - position at which to start the search, default 0
Returns position of text, or -1 if nothing found.
lastIndexOf (txt[, from]) Finds last occurrence of text. Parameters:
  • txt - text for searching
  • from - position at which to start the search, default 0
Returns position of text, or -1 if nothing found.
startsWith(strSearch [,pos]) Returns true if the given characters are found at the beginning of the string; otherwise, false. Default value of the pos parameter is 0.
endsWith(strSearch [,len]) Returns true if the given characters are found at the end of the string; otherwise, false. The len parameter specifies the desired length of this string.
match(regexp) Retrieves the result of matching a string against a regular expression. Returns null if no matches are found, or Array with following content:
  • If the g flag is used, all results matching the complete regular expression will be returned, but capturing groups will not.
  • If the g flag is not used, only the first complete match and its related capturing groups are returned. In this case, the returned item will have additional properties as described below.
matchAll(regexp) Returns an iterator of all results matching a string against a regular expression, including capturing groups. The regexp parameter must have the g flag, otherwise a TypeError will be thrown.
search(regexp) Searches first match between the regular expression and the given string. Returns position of match, or -1 if nothing found.
replace(regexp|substr, newSubstr|func)

Returns a new string with some or all matches of a pattern replaced with a replacement string specified by the newSubstr or generated by the func function.

If pattern is a string, only the first occurrence will be replaced.

The newSubstr parameter can include the following special replacement patterns:

  • $$ - inserts $
  • $& - inserts the matched substring
  • $` - inserts the portion of the string that precedes the matched substring
  • $' - inserts the portion of the string that follows the matched substring
  • $n - inserts the nth parenthesized submatch string, i.e. capture group
  • $<name> - inserts the named capture group (be careful, not all browsers support this)
replaceAll(regexp|substr, newSubstr|func) Returns a new string with all matches of a pattern replaced by a replacement.

If the replacement string specified by regular expression, it must have flag g.

charAt(pos) Returns character at pos position.
charCodeAt(pos) Returns the unicode of the character at pos position.
slice(from[,to ]) Extracts a section of a string and returns it as a new string, without modifying the original string. Parameters:
  • from - position at which to begin extraction. Negative value considers as str.length+from
  • to - position before which to end extraction. Negative value considers as str.length+from
substring   (from [, to]) Extracts a part of a string and returns it as a new string. Parameters:
  • from - position of the first character to include in the returned substring
  • to - position of the first character to exclude from the returned substring. If omitted, then characters are extracted to the end of the string.
substr   (from [, len]) Extracts a part of a string and returns it as a new string. Parameters:
  • from - position of the first character to include in the returned substring. If it is negative value, than position counts from the end of the string.
  • len - number of characters to extract. If omitted, then characters are extracted to the end of the string.
split   ([delim[, limit]]) Splits string into an array of strings. Parameters:
  • delim - string or a regular expression that used as delimiter.
  • limit - maximum size of resulting array.
concat   (str [, ...strN]) Concatenates the string parameters into one new string.
repeat(count) Returns a new string containing the specified number of copies of the given string.
trim() Returns new string with stripped whitespace from both sides. Most browsers support methods trimStart(), trimLeft(), trimEnd() and trimRight for removing whitespace from one side.
padStart(toLen [, padstr]) Pads the current string with another string until the resulting string reaches the given length. The padding is applied from the start of the current string. Default value of the padstr parameter is space.
padEnd(toLen [, padstr]) Pads the current string with another string until the resulting string reaches the given length. The padding is applied from the end of the current string. Default value of the padstr parameter is space.
toUpperCase() Returns new string with upper case characters.
toLowerCase() Returns new string with lower case characters.