How to Search a String for a Pattern in JavaScript ? Last Updated : 12 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are two ways to search a string for a pattern in JavaScript.1. Using string search() MethodThe JavaScript string search() method is used to serch a specified substing from 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 Replace All Occurrences of a String in JavaScript? Here are different approaches to replace all occurrences of a string in JavaScript.1. Using string.replace() MethodThe string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.Syntaxstr.replace(replac 2 min read How to modify a string in JavaScript ? JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. Representing a String: 1. Using double 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 Array Search And Remove String. Arrays are widely used to store and manipulate collections of data. Sometimes we may need to search for a specific string within an array and remove it. This operation can be useful in various cases, such as filtering out unwanted items, managing user inputs, or processing data from an external sour 2 min read How to Iterate Over Characters of a String in JavaScript ? There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.Syntaxfor (statement 1 ; statement 2 2 min read Like