How to Search a String for a Pattern in JavaScript ? Last Updated : 13 Jun, 2025 Comments Improve Suggest changes Like Article Like Report Here are two ways to search a string for a pattern in JavaScript.1. Using the string search() MethodThe JavaScript string search() method is used to search a specified substring within the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the pattern is not found.Syntaxstr.search( pattern ) JavaScript // Given String let s = "GeeksforGeeks"; // Defining the patterns using regular expressions. let p1 = /G/; // pattern to find G let p2 = /k/; // pattern to find k let p3 = /p/; // pattern to find p // Get the position of patten using string.search() // and Display the output console.log("The index of G is:", s.search(p1)); console.log("The index of k is:", s.search(p2)); console.log("The index of p is:", s.search(p3)); OutputThe index of G is: 0 The index of k is: 3 The index of p is: -1 2. Using string match() MethodThe string match() method is used to get all matching substrings. It returns an arrray of all substrings if the pattern is found or null if no match is found.Syntaxstring.match( expression ) JavaScript // Given String let s = "GeeksforGeeks"; // Get the position of patten using string.search() // and Display the output console.log("The Occurence of Ge:", s.match(/rGe/g)); console.log("The Occurence of ee:", s.match(/ee/g)); console.log("The Occurence of sf:", s.match(/re/g)); OutputThe Occurence of Ge: [ 'rGe' ] The Occurence of ee: [ 'ee', 'ee' ] The Occurence of sf: null Comment More infoAdvertise with us Next Article How to Search a String for a Pattern in JavaScript ? S souvikm02 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads How to Search for Multiple Words Within a String or Array in JavaScript? To search for multiple words within a string or an array in JavaScript, you can use different approaches depending on the data structure. Hereâs how to perform the search effectively:1. Searching for Multiple Words in a StringTo check if a string contains multiple words, you can use String.prototype 3 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read JavaScript - How To Count String Occurrence in String? Here are the various methods to count string occurrence in a string using JavaScript.1. Using match() Method (Common Approach)The match() method is a simple and effective way to count occurrences using a regular expression. It returns an array of all matches, and the length of the array gives the co 3 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read How to test a string as a literal and as an object in JavaScript ? In this article, we learn how to test a string as a literal and as an object using JavaScript. What is JavaScript Literal?Literals are ways of representing fixed values in source code. In most programming languages, values are notated by integers, floating-point numbers, strings, and usually by bool 3 min read JavaScript - Convert a String to Boolean in JS Here are different ways to convert string to boolean in JavaScript.1. Using JavaScript == OperatorThe == operator compares the equality of two operands. If equal then the condition is true otherwise false. Syntaxconsole.log(YOUR_STRING == 'true');JavaScriptlet str1 = "false"; console.log(str1 == 'tr 3 min read JavaScript - How to Check if a String Contains Double Quotes? Here are the different methods to check if a string contains double quotes.1. Using includes() MethodThe includes() method is the simplest way to check if a string contains a specific substring, including double quotes. This method returns true if the substring is found and false otherwise.JavaScrip 3 min read How to find if an array contains a specific string in JavaScript/jQuery ? In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array 5 min read JavaScript - How to Check if a String "StartsWith" Another String in JavaScript? Here are the various methods to check if a string starts with another string in JavaScript1. Using String.startsWith() MethodThe most widely used method is the startsWith() method, built into JavaScript. This method is simple to use, highly readable, and performs the task efficiently.JavaScriptlet s 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read Like