Searching for Text in a String with a Custom Function in JavaScript

2 min read .

In web or JavaScript application development, we often need to search for specific text within a larger string. While JavaScript provides built-in methods like indexOf or search, sometimes we need more control over the search results. How to create a custom findText function in JavaScript to find and return the surrounding text around the searched keyword.

1. Introduction to the findText Function

The findText function is designed to search for a substring within a larger string and return the surrounding text around the position where the substring was found. This function allows you to specify how much text to include on either side of the found substring.

Here is the implementation of the findText function:

const findText = (text, f, space = 100) => {
    let tf = f;
    let find = text.search(tf);

    if (find > -1) {
        let cl = 0;
        if ((find - space) > 0) {
            cl = find - space;
        } else {
            cl = 0;
        }

        let cr = tf.length;
        if ((text.length - find) > space) {
            cr = tf.length + find + space;
        } else {
            cr = text.length;
        }

        return text.slice(cl, cr);
    } else {
        return 'not found';
    }
};

2. Explanation of the Implementation

Let’s discuss the steps in the findText function:

  • text.search(tf): Searches for the substring tf within the string text. If found, search returns the index of the first occurrence of the substring. If not found, search returns -1.

  • Determining the Left Boundary (cl):

    • If the position found (find) minus space (number of characters on the left) is greater than zero, then cl (current left) is set to that position.
    • If not, cl is set to 0 (start of the string).
  • Determining the Right Boundary (cr):

    • If the length of the string minus the position found is greater than space, then cr (current right) is set to the length of the substring plus the position found plus space.
    • If not, cr is set to the length of the string.
  • Returning the Result: If the substring is found, the function returns the part of the string defined by cl and cr using slice. If not found, the function returns 'not found'.

3. Example Usage

Here is an example of using the findText function to search for text within a string:

const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
const substring = "consectetur";
const result = findText(text, substring, 20);
console.log(result);

The output will be:

"m dolor sit amet, consectetur adipiscing elit. Sed do eiu"

In this example, findText finds the substring "consectetur" and returns 20 characters before and after it.

4. When the Substring is Not Found

If the searched substring is not found within the larger string, the function will return 'not found':

const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
const substring = "python";
const result = findText(text, substring);
console.log(result);  // Output: 'not found'

5. Benefits of the findText Function

The findText function is useful in various situations, such as searching for text in large documents, logs, or text data analysis. With the ability to specify the scope of the returned result, this function provides more flexibility than JavaScript’s built-in methods.

Conclusion

The findText function is an effective tool for searching and extracting text around a searched substring. By using this approach, you can easily locate text and obtain additional context around it, which is particularly useful in many application development and data processing scenarios.

See Also

chevron-up