JavaScript String search() Method - JavaScript Methods for Searching Strings



JavaScript String Search

  • JavaScript Search Methods :
    • String indexOf()
    • String lastIndexOf()
    • String search()
    • String match()
    • String matchAll()
    • String includes()
    • String startsWith()
    • String endsWith()
  • JavaScript Search Methods and definition :
    • The indexOf() method returns the index of (position of) the first occurrence of a string in a string
    • The lastIndexOf() method returns the index of the last occurrence of a specified text in a string
    • The search() method searches a string for a string (or a regular expression) and returns the position of the match
    • The match() method returns an array containing the results of matching a string against a string (or a regular expression).
    • The matchAll() method returns an iterator containing the results of matching a string against a string (or a regular expression).
    • The includes() method returns true if a string contains a specified value.Otherwise it returns false.
    • The startsWith() method returns true if a string begins with a specified value.Otherwise it returns false
    • The endsWith() method returns true if a string ends with a specified value.Otherwise it returns false
javascript-search-string

Sample Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p id="demo1"></p>
    <p id="demo2"></p>
    <p id="demo3"></p>
    <p id="demo4"></p>
    <p id="demo5"></p>
    <p id="demo6"></p>
    <p id="demo7"></p>
    <p id="demo8"></p>
    <script>
        let text = "welcome to kaashiv infotech!";
        document.getElementById("demo1").innerHTML = text.indexOf("kaashiv");
        document.getElementById("demo2").innerHTML = text.lastIndexOf("kaashiv");
        document.getElementById("demo3").innerHTML = text.search("kaashiv");
        document.getElementById("demo4").innerHTML = text.match("kaashiv");
        document.getElementById("demo5").innerHTML = text.matchAll("kaashiv");
        document.getElementById("demo6").innerHTML = text.includes("kaashiv");
        document.getElementById("demo7").innerHTML= text.startsWith("kaashiv");
        document.getElementById("demo8").innerHTML = text.endsWith("kaashiv");

        
    </script>
</body>
</html>

Output

11
11
11
kaashiv
[object RegExp String Iterator]
true
false
false

Related Searches to JavaScript String search() Method - JavaScript Methods for Searching Strings