Dorokhov.codes
String
Creating strings
const name = "Andrew";
Interpolation
const message = `Hello, ${name}!`; // Only this type of quotes.
Properties
s.length
Methods
s.toUpperCase();
s.toLowerCase();
s.indexOf('world'); // Find index of a substring.
s.trim(); // Removes whitespace from both ends of a string and returns a new string.
s.split(', '); // Split a string and return an array.
s.charCodeAt(); // Get a char code by an index.
Working with substrings:
// slice(indexStart, indexEnd)
s.slice(7, 10); // Substring from index 7 to index 10.
s.slice(7); // Substring from index 7 to the end of the string.
// substring(indexStart, indexEnd)
s.substring(7, 10); // Substring from index 7 to index 10.
s.substring(7); // Substring from index 7 to the end of the string.
// substr(start, length)
s.substr(7, 3); // Substring from index 7 and 3 symbols length
Get a char:
s[3]; // The fourth char of the string.
Static methods:
String.fromCharCode(); // Get a char(s) by its code(s).
Regular expressions
Flags:
i
- case insensitive.g
- global (all matches).m
- multiline.
Special symbols:
.
- any character.
Classes:
\d
- numbers.\D
- not numbers.\w
- word characters.\W
- not word characters.\s
- space characters.\S
- not space characters.
new RegExp('pattern', 'flags');
// Or:
/pattern/flags;
Find something in a string (get position):
const s = "Ann";
s.search(/n/);
Get array of matches:
s.match(/n/); // Only one.
s.match(/n/g); // All.
Replace substring:
s.replace(/n/g, '*');
Test regexp:
someRegExp.test(str); // Returns true of false.
Conversion to a string
String(15); // We will get a string type.
15 + ''; // We will get a string type.