자바스크립트 문자열 검색(Search)

One of the operations commonly performed on strings in JavaScript is searching for specific substrings within a string. This process, known as string searching or string matching, allows you to locate the position of a given substring or find all occurrences of a particular pattern within a string. In this blog post, we’ll explore different techniques to perform string searching in JavaScript.

Method 1: indexOf()

The indexOf() method is a built-in function in JavaScript that allows you to find the index of the first occurrence of a substring within a string. It returns the index of the first character of the found substring or -1 if the substring is not found.

Here’s an example of using indexOf() to search for a substring:

const str = "Hello, World!";
const searchStr = "World";
const result = str.indexOf(searchStr);

console.log(result); // Output: 7

In the above example, the indexOf() method is used to find the position of the substring “World” within the string “Hello, World!”. The method returns 7, indicating that the substring was found starting at index 7.

Method 2: lastIndexOf()

The lastIndexOf() method works similar to indexOf(), but it searches for the last occurrence of a substring within a string instead of the first occurrence. It returns the index of the first character of the last found substring or -1 if the substring is not found.

const str = "Hello, World!";
const searchStr = "o";
const result = str.lastIndexOf(searchStr);

console.log(result); // Output: 8

In the above example, the lastIndexOf() method is used to find the position of the last occurrence of the substring “o” within the string “Hello, World!”. The method returns 8, indicating that the last occurrence of the substring was found starting at index 8.

Method 3: Regular Expressions (Regex)

Regular expressions provide a powerful way to perform complex string searching and matching operations in JavaScript. You can use the exec() method or the test() method of the RegExp object to search for substrings based on patterns defined by regular expressions.

Here’s an example of using regular expressions to search for a substring:

const str = "Hello, World!";
const regex = /world/i;
const result = regex.test(str);

console.log(result); // Output: true

In the above example, a case-insensitive regular expression /world/i is used to search for the substring “world” within the string “Hello, World!”. The test() method returns true because the substring was found.

Conclusion

Searching for substrings within strings is a common operation in JavaScript. In this blog post, we explored three different methods to perform string searching: indexOf(), lastIndexOf(), and regular expressions. Each method has its own advantages and use cases. Depending on the requirements of your application, you can choose the most suitable method for your string searching needs.

Remember to always refer to the documentation and experiment with different approaches to get the desired results in your JavaScript projects.