Javascript String Find and Replace Functions
To search for a string inside a string, there are several functions:
indexOf( searchString ) and lastIndexOf( searchString )
indexOf() will return the index of the first occurrence of searchString in the string. If searchString is not found, then -1 is returned.
var string = "Hello, World!";
console.log( string.indexOf("o") ); // 4
console.log( string.indexOf("foo") ); // -1
Similarly, lastIndexOf() will return the index of the last occurrence of searchstring or -1 if not found.
var string = "Hello, World!";
console.log( string.lastIndexOf("o") ); // 8
console.log( string.lastIndexOf("foo") ); // -1
includes( searchString, start )
includes() will return a boolean that tells whether searchString exists in the string, starting from index start (defaults to 0). This is better than indexOf() if you simply need to test for existence of a substring.
var string = "Hello, World!";
console.log( string.includes("Hello") ); // true
console.log( string.includes("foo") ); // false
replace( regexp|substring, replacement|replaceFunction )
replace() will return a string that has all occurrences of substrings matching the RegExp regexp or stringsubstring with a string replacement or the returned value of replaceFunction.
Note that this does not modify the string in place, but returns the string with replacements.
var string = "Hello, World!";
string = string.replace( "Hello", "Bye" );
console.log( string );
string = string.replace( /W.{3}d/g, "Universe" );
console.log( string );
Output
"Bye, Universe!"
replaceFunction can be used for conditional replacements for regular expression objects (i.e., with use with regexp).