String Search Methods
| String indexOf() String lastIndexOf() String search() See Also: Basic String Methods String Templates | String match() String matchAll() String includes() String startsWith() String endsWith() |
|---|
// index can have start pos, search can regex exp
// returns -1 if the string is not found:
let index = text.indexOf("locate");
let index = text.indexOf("locate", 15);
// searches a string for a string (or a regular expression)
let index = text.search("locate");
let index = text.search(/locate/);
// returns an array containing the results of matching a string against a string (or a regular expression).
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain"); // ain
text.match(/ain/); // ain
text.match(/ain/g); // ain,ain,ain
text.match(/ain/gi); // ain,AIN,ain,ain
// The `includes()` method returns true if a string contains a specified value.
let text = "Hello world, welcome to the universe.";
text.includes("world");
text.includes("world", 12);
text.startsWith("world", 5);