Your Page Title
🔍

    JavaScript String Search

    In JavaScript, working with text often requires searching for specific words, phrases, or characters within a string. JavaScript provides several built-in string methods for searching, matching, and locating text.

    This tutorial will teach you how to use key string search methods effectively, including indexOf(), lastIndexOf(), search(), includes(), match(), startsWith(), and endsWith().


    1. indexOf()

    The indexOf() method returns the first position of a specified value in a string. If the value is not found, it returns -1.

    javascriptCopyEditlet text = "JavaScript is awesome!";
    let position = text.indexOf("is");
    console.log(position); // 11
    

    You can also pass a second argument to start searching from a specific index:

    javascriptCopyEditconsole.log(text.indexOf("a", 2)); // 3
    

    2. lastIndexOf()

    lastIndexOf() works like indexOf(), but it searches from the end of the string.

    javascriptCopyEditlet phrase = "I love JavaScript and Java";
    console.log(phrase.lastIndexOf("Java")); // 22
    

    If not found, it returns -1.


    3. includes()

    includes() checks if a string contains a specified substring. It returns true or false.

    javascriptCopyEditlet msg = "Hello, world!";
    console.log(msg.includes("world")); // true
    console.log(msg.includes("World")); // false (case-sensitive)
    

    You can also specify a start position:

    javascriptCopyEditconsole.log(msg.includes("Hello", 5)); // false
    

    4. search()

    The search() method searches for a match using a regular expression and returns the position of the first match. Unlike indexOf(), search() only accepts regular expressions.

    javascriptCopyEditlet sentence = "The quick brown fox jumps over the lazy dog.";
    let pos = sentence.search(/fox/);
    console.log(pos); // 16
    

    If no match is found, it returns -1.


    5. match()

    The match() method searches a string for a match using a regular expression and returns the matched results.

    Basic match:

    javascriptCopyEditlet result = sentence.match(/fox/);
    console.log(result[0]); // "fox"
    

    Match all occurrences (with global flag g):

    javascriptCopyEditlet text = "cat bat rat cat";
    let matches = text.match(/cat/g);
    console.log(matches); // ["cat", "cat"]
    

    6. startsWith()

    This method checks if a string starts with the given substring.

    javascriptCopyEditlet greeting = "Hello, world!";
    console.log(greeting.startsWith("Hello")); // true
    console.log(greeting.startsWith("world")); // false
    

    You can also pass a position to start checking from:

    javascriptCopyEditconsole.log(greeting.startsWith("world", 7)); // true
    

    7. endsWith()

    This method checks if a string ends with the given substring.

    javascriptCopyEditlet filename = "report.pdf";
    console.log(filename.endsWith(".pdf")); // true
    

    You can also check against a portion of the string:

    javascriptCopyEditconsole.log(filename.endsWith("report", 6)); // true
    

    8. Comparing Search Methods

    MethodReturnsUse Case
    indexOf()Index or -1Find position of a substring
    lastIndexOf()Last index or -1Find last occurrence
    includes()true or falseCheck if text exists
    search()Index or -1Search using regular expressions
    match()Array or nullExtract matched values
    startsWith()true or falseCheck string start
    endsWith()true or falseCheck string end

    9. Case Sensitivity

    All string search methods in JavaScript are case-sensitive by default.

    javascriptCopyEditlet word = "JavaScript";
    console.log(word.includes("java")); // false
    

    To do case-insensitive searching, use regular expressions with the i flag:

    javascriptCopyEditconsole.log(word.search(/java/i)); // 0
    

    10. Real-World Example: Simple Search Filter

    javascriptCopyEditlet products = ["Laptop", "Phone", "Tablet", "Monitor"];
    let keyword = "ph";
    
    let filtered = products.filter(item => item.toLowerCase().includes(keyword.toLowerCase()));
    console.log(filtered); // ["Phone"]
    

    This technique is useful in search bars and filter systems.


    Conclusion

    JavaScript offers several powerful string search methods. Here’s a quick recap:

    • Use indexOf() and lastIndexOf() for positions.
    • Use includes() to check if a string exists.
    • Use search() and match() for regular expressions.
    • Use startsWith() and endsWith() to check string boundaries.

    Leave a Reply

    Your email address will not be published. Required fields are marked *