JavaScript string search is a crucial element that you will need if the project you’re working on calls for you to extract specific data from sizable data files. Also, you can search for a particular string or regular expression using any of the string search methods available in JavaScript. Again, you can use this method with a specific instance of the String class because it is a property of the String object.
Read on to learn about the top JavaScript string functions you can use to fetch chunks of data from large files or manipulate string documents.
1. String search()
You can use the JavaScript method string search() to find a match between regular expressions and a specified string object. Also, this method only accepts the regular expression as an object as the single parameter A.
// The code will tell you the position of th word you identify in the search method in number format
let strngSach = "Please uncover the geolocation of 'the' in integer format!";
let sx =strngSach.search("the");
console.log(sx) // output is 15
// The code will tell you the position of the word you identify in the search method in number format
let strngSach = "Please uncover the geolocation of 'the' in integer format!";
let sx =strngSach.search("the");
console.log(sx) // output is 15 // stg takes a string input.
var stg = "Fifa worldcup semi finalist";
// Taking a regular expression.
var wc1= /w/;
var wc2= /F/;
var wc3= /s/;
var wc4= /g/;
var wc5= /a/;
// the code below prints the index of matching alphabets in the var stg
console.log(stg.search(wc1)); // output 15
console.log(stg.search(wc2)); // output 5
console.log(stg.search(wc3)); // output 0
console.log(stg.search(wc4)); // output 14
console.log(stg.search(wc5)); // output -1
2. String match()
The built-in JavaScript string match() function allows you to find a string that matches any regular expression. And with the help of these methods, you can retrieve the outcome of a string match and get back an array with the results.
// assign a string value to the variable tet1
let tet1 = "you never when it is your time to shine";
// declare the word we want to match
const myArr1 = tet1.match("hin");
// g in this code means global
const myArr2 = tet1.match(/ne/g);
console.log(myArr1.length + " - " + myArr1); // output - hin
console.log(myArr2.length + " . " + myArr2); // output . ne,ne
3. String indexOf()
Using the lastIndexOf() method, you can determine the string’s first instance of a given text’s index. Again, these methods locate the substring within the string. And because the indexOf() method is a property of the String object, a specific instance of the String class must be used to call it.
let text = "It is not coming on "as" it is supposed to!";
console.log( text.indexOf("as"));
4. String lastIndexOf()
To find the last occurrence of a given text in the string, use the lastIndexOf() method. This method will return the word’s most recent index, or more precisely, the zero-based index position of the string’s most recent occurrence inside this instance.
let text = "It is not coming on "as" it is supposed to!";
console.log( text.lastIndexOf("as"));
5. String includes()
With the help of the string include() function, you can quickly determine whether a string of data in your program contains a particular character, symbol, or word. Also, when they do, the includes() method returns true; when they don’t, it returns false.
let dTxt = "Hello Simeon Peter, welcome to the real planet.";
var d = dTxt.includes("real");
console.log(d);
6. String startsWith()
The quickest way to find words that begin with a particular value is to use the string startsWith(). If a string begins with the value specified, the startsWith() method returns true.
// initialize variable
let checkWord = "JavaScript is a popular programming language every aspiring web developer should not overlook.";
// introduce the the word you want to know if it started with
let checkWord1 = checkWord.startsWith("J")
console.log(checkWord1) // output is true
8. String endsWith()
If the string in your query ends with the word or character you enter, the endsWith() function will return a boolean value.
let checkWord = "JavaScript is a popular programming language every aspiring web developer should not overlook.";
let checkWord1 = checkWord.endsWith("overlook.")
console.log(checkWord1)
Conclusion
You discovered in this article what JavaScript string search methods are—one of the crucial elements you’ll need to create intricate programs.