JavaScript String Search
Methods for searching and finding text in strings
🔍 What is String Search?
String search methods help you find specific text within strings. You can search for characters, words, or patterns and get their positions or check if they exist.
let text = "Hello World, Welcome to JavaScript";
// Search for text
let position = text.indexOf("World"); // 6
let exists = text.includes("JavaScript"); // true
let starts = text.startsWith("Hello"); // true
console.log(position, exists, starts);
Output:
6 true true
String Search Methods
indexOf()
Find first occurrence position
text.indexOf("World") // 6
text.indexOf("xyz") // -1
lastIndexOf()
Find last occurrence position
text.lastIndexOf("o") // 15
text.lastIndexOf("xyz") // -1
includes()
Check if text exists
text.includes("World") // true
text.includes("xyz") // false
startsWith()
Check if text starts with
text.startsWith("Hello") // true
text.startsWith("World") // false
🔹 indexOf() and lastIndexOf()
Find the position of text within a string:
let text = "Hello World, Hello Universe";
// indexOf() - finds first occurrence
let first = text.indexOf("Hello"); // 0
let firstO = text.indexOf("o"); // 4
let notFound = text.indexOf("xyz"); // -1
// lastIndexOf() - finds last occurrence
let last = text.lastIndexOf("Hello"); // 13
let lastO = text.lastIndexOf("o"); // 15
// Search from specific position
let fromPos = text.indexOf("o", 5); // 7 (starts search from index 5)
console.log("First 'Hello':", first);
console.log("First 'o':", firstO);
console.log("Not found:", notFound);
console.log("Last 'Hello':", last);
console.log("Last 'o':", lastO);
console.log("'o' from position 5:", fromPos);
Output:
First 'Hello': 0
First 'o': 4
Not found: -1
Last 'Hello': 13
Last 'o': 15
'o' from position 5: 7
🔹 includes() Method
Check if a string contains specific text:
let text = "JavaScript is awesome and powerful";
// Basic includes
let hasJS = text.includes("JavaScript"); // true
let hasJava = text.includes("Java"); // true
let hasPython = text.includes("Python"); // false
// Case sensitive
let hasjs = text.includes("javascript"); // false (case sensitive)
// Search from specific position
let hasAwesome = text.includes("awesome", 15); // true
let hasIs = text.includes("is", 15); // false (not found after position 15)
console.log("Has 'JavaScript':", hasJS);
console.log("Has 'Java':", hasJava);
console.log("Has 'Python':", hasPython);
console.log("Has 'javascript' (lowercase):", hasjs);
console.log("Has 'awesome' from pos 15:", hasAwesome);
console.log("Has 'is' from pos 15:", hasIs);
Output:
Has 'JavaScript': true
Has 'Java': true
Has 'Python': false
Has 'javascript' (lowercase): false
Has 'awesome' from pos 15: true
Has 'is' from pos 15: false
🔹 startsWith() and endsWith()
Check if a string starts or ends with specific text:
let text = "JavaScript Programming Tutorial";
// startsWith() - check beginning
let startsWithJS = text.startsWith("JavaScript"); // true
let startsWithProg = text.startsWith("Programming"); // false
let startsWithProg2 = text.startsWith("Programming", 11); // true (from position 11)
// endsWith() - check ending
let endsWithTutorial = text.endsWith("Tutorial"); // true
let endsWithProg = text.endsWith("Programming"); // false
let endsWithJS = text.endsWith("JavaScript", 10); // true (within first 10 chars)
console.log("Starts with 'JavaScript':", startsWithJS);
console.log("Starts with 'Programming':", startsWithProg);
console.log("Starts with 'Programming' from pos 11:", startsWithProg2);
console.log("Ends with 'Tutorial':", endsWithTutorial);
console.log("Ends with 'Programming':", endsWithProg);
console.log("Ends with 'JavaScript' in first 10 chars:", endsWithJS);
Output:
Starts with 'JavaScript': true
Starts with 'Programming': false
Starts with 'Programming' from pos 11: true
Ends with 'Tutorial': true
Ends with 'Programming': false
Ends with 'JavaScript' in first 10 chars: true
🔹 search() Method
Search using regular expressions:
let text = "JavaScript is Great! JavaScript rocks!";
// search() with string (similar to indexOf)
let searchString = text.search("JavaScript"); // 0
// search() with regular expression
let searchRegex = text.search(/great/i); // 14 (case insensitive)
let searchNumber = text.search(/\d/); // -1 (no numbers found)
let searchWord = text.search(/\bGreat\b/i); // 14 (whole word, case insensitive)
// Complex patterns
let searchPattern = text.search(/Java\w+/); // 0 (Java followed by word characters)
console.log("Search 'JavaScript':", searchString);
console.log("Search 'great' (case insensitive):", searchRegex);
console.log("Search for numbers:", searchNumber);
console.log("Search whole word 'Great':", searchWord);
console.log("Search 'Java' + word chars:", searchPattern);
Output:
Search 'JavaScript': 0
Search 'great' (case insensitive): 14
Search for numbers: -1
Search whole word 'Great': 14
Search 'Java' + word chars: 0
🔹 match() Method
Extract matches using regular expressions:
let text = "Contact us at: [email protected] or [email protected]";
// match() with string
let matchString = text.match("email");
console.log("Match 'email':", matchString);
// match() with regular expression
let matchEmail = text.match(/\w+@\w+\.\w+/); // First email
console.log("First email:", matchEmail[0]);
// match() with global flag
let matchAllEmails = text.match(/\w+@\w+\.\w+/g); // All emails
console.log("All emails:", matchAllEmails);
// match() with groups
let emailPattern = /(\w+)@(\w+)\.(\w+)/g;
let emailParts = text.match(emailPattern);
console.log("Email parts:", emailParts);
Output:
Match 'email': ["email", index: 20, input: "Contact us at: [email protected] or [email protected]", groups: undefined]
First email: [email protected]
All emails: ["[email protected]", "[email protected]"]
Email parts: ["[email protected]", "[email protected]"]