How to Search a String in JavaScript Using Search() Method?

Do you want to know how to search all occurrences in a string or character in JavaScript using the search() method? Read on!

Are you ready to explore the power of the JavaScript search() method?

In this article, we will show you how to use search() to find matches in a string and understand the difference between search() and indexOf().

Aside from that, you will discover the other JavaScript methods for string searching, packed with examples and explanations.

What is search() method?

The search() method is a built-in method in JavaScript that allows you to search for a match between a regular expression and a string.

It returns the index of the first match found in the string, or -1 if no match is found. This method is case-sensitive, meaning that it will treat uppercase and lowercase characters as distinct when searching for a match.

If the search value is a string, it is automatically converted to a regular expression before the search is performed.

Syntax

 string.search(searchValue)

Parameter

searchValue (Required)

The searchValue parameter of the search() method is the value that you want to search for within the string. This value can be either a regular expression or a string.

If you provide a string as the searchValue, it will be automatically converted into a regular expression before the search is performed.

Return value

The search() method returns the index of the first match found in the string, or -1 if no match is found.

How to search all occurrences in a string in JavaScript?

There are several ways to search for all occurrences in a string in JavaScript.

One way is to use the search() method as what we have mentioned earlier, which searches a string for a specified value and returns the position of the match.

Here’s an example of how to use the search() method:

let sampletext = "Hi Welcome to Itsourcecode";
let result = sampletext.search("Itsourcecode"); ✅
console.log(result)

Output:

14

Here’s an example of how to use the search() method for case insensitive:

let sampletext = "Hi Welcome to Itsourcecode";
let result = sampletext.search(/itsourcecode/i); ✅
console.log(result);

The “i” flag at the end of the regular expression makes the search case-insensitive.

Output:

14

What is the difference between search() and indexOf()?

The search() and indexOf() methods are both used to search for a substring within a string in JavaScript. Nevertheless, there are few differences between the two methods:

The search() method takes a regular expression as an argument, while the indexOf() method takes a string as an argument.

It simply means that you can use more complex search patterns with the search() method, while the indexOf() method is limited to searching for exact matches of the specified string.

The indexOf() method can take a second argument, which specifies the position in the string where the search should begin.

This allows you to start searching from a specific point in the string. However, search() method doesn’t have this option.

What are the different search string methods in JavaScript?

Here are some examples that demonstrate the use of different methods for searching for a character, string or a substring within another string in JavaScript:

Method 1: match() method

let samplestr = "Itsourcecodes provide free sourcecodes";
let result = samplestr.match(/code/g); ✅
console.log(result); 

In our first example, we use the match() method to search for all occurrences of the regular expression /code/ within the string.

The method returns an array containing all matches.

Output:

[ 'code', 'code' ]

Method 2: includes() method

let samplestr = "Welcome to Itsourcecode!";
let result = samplestr.includes("Itsourcecode"); ✅
console.log(result);

In our second example, we use the includes() method to check if the string “Welcome to Itsourcecode!” contains the substring “Itsourcecode.”

The method returns a boolean value indicating whether or not the substring was found.

Output:

true

Method 3: startsWith() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.startsWith("Hi"); ✅
console.log(result); 

In our third example, we use the startsWith() method to check if the string “Hi, Welcome to Itsourcecode!” starts with the substring “Hello.”

The method returns a boolean value indicating whether or not the string starts with the specified substring.

Output:

true

Method 4: endsWith() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.endsWith("Itsourcecode!"); ✅
console.log(result); 

In our 4rth example, we use the endsWith() method to check if the string “Hi, Welcome to Itsourcecode!” ends with the substring “Itsourcecode!

The method returns a boolean value indicating whether or not the string ends with the specified substring.

true

Method 5: indexOf() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.indexOf("Itsourcecode!"); ✅
console.log(result); 

In our 5th example, we use the indexOf() method to search for the first occurrence of the substring “Itsourcecode” within the string “Hi, Welcome to Itsourcecode!

The method returns the index of the first occurrence of the substring.

15

Method 6: lastIndexOf() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.lastIndexOf("t");
console.log(result); 

In our 6th example, we use the lastIndexOf() method to search for the last occurrence of the character “t” within the string “Hi, Welcome to Itsourcecode!

The method returns the index of the last occurrence of the character.

Conclusion

The JavaScript search() method is a useful tool for searching strings or characters, allowing you to find matches using regular expressions or simple strings.

We have explained the syntax, parameters, and return value of the search() method, and provided examples of how to use it to search for a match between a regular expression and a string.

Unlike indexOf(), which searches for exact matches, search() provides greater flexibility in specifying search patterns.

Moreover, we also explored other string searching methods such as match(), includes(), startsWith(), endsWith(), indexOf(), and lastIndexOf(), and provides examples of how to use each of these methods.

We are hoping that this article provides you with enough information. That will help you understand the search string in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment