HOWTO · JavaScript

How to Filter String in JavaScript

Filter an array of strings in JavaScript with filter(), includes(), indexOf(), or RegExp.test(). Learn what each callback returns and how to collect every match.

On this page

To select strings from an array, call filter() with a callback that tests each string. It returns a new array containing every element for which the callback returns a truthy value, in the same order as the source. It does not change the original array. The callback can use a condition, literal substring search, or regular expression.

Choose filter() when you need all matching strings. find() returns the first matching string (or undefined if there is no match), while findIndex() returns the position of the first match (or -1). These methods do not return a count of all matches.

How filter() selects strings

filter() calls its callback once for each element in the array and uses the callback result to decide whether to keep that element. The callback can return a Boolean, as with includes(), or another value that JavaScript treats as truthy or falsy. The result is always a new array; if nothing matches, it is an empty array. The source array remains available with its original contents.

Filter strings with a predicate

A predicate is a function that answers whether an element meets a condition. For example, this predicate keeps strings with at most five characters. Declare both the source and result so the example can run as written.

Run this example:

const words = ['abadef', 'sbade', 'psadbe'];
const shortWords = words.filter((word) => word.length <= 5);
console.log(JSON.stringify(shortWords));
["sbade"]

The callback receives each string in turn. Because only sbade has five or fewer characters, the returned array contains that string. The original words array is not modified. Change the predicate to express the rule your data needs, such as checking a prefix or comparing a string’s length.

Find strings containing text with includes()

Use includes() for a literal substring search. It returns true when the string contains the requested text and false otherwise, so it can be passed directly as the filter() callback. Its argument is a string, not a regular expression. Matching is case-sensitive: searching for "ef" does not match "EF".

const words = ['abadef', 'sbade', 'psadbe'];
const matches = words.filter((word) => word.includes('ef'));
console.log(JSON.stringify(matches));
["abadef"]

Use this option when the search text should be treated literally. If capitalization should not matter, normalize both values first, for example by calling toLowerCase() on the string and search text.

Match substrings with indexOf()

indexOf() also searches for literal text, but it returns a number rather than a Boolean. The number is the first character position where the substring begins; when it is absent, the result is -1. Compare the result with -1 to turn it into a clear predicate. filter() still returns the matching array elements, not their character positions.

const words = ['abadef', 'sbade', 'psadbe'];
const matches = words.filter((word) => word.indexOf('ad') !== -1);
console.log(JSON.stringify(matches));
["abadef","sbade","psadbe"]

The comparison keeps a string only when indexOf() found the substring. This is useful in code that already uses indexOf() or when the position itself is also useful; for a direct yes-or-no substring check, includes() is usually easier to read.

Match patterns with RegExp.test()

Use RegExp.test() when the match follows a pattern, such as a prefix, digit, or one of several alternatives. It returns a Boolean, which makes it suitable as the callback passed to filter(). The expression /ba/ below keeps strings containing those consecutive letters.

const words = ['abadef', 'sbade', 'psadbe'];
const pattern = /ba/;
const matches = words.filter((word) => pattern.test(word));
console.log(JSON.stringify(matches));
["abadef","sbade"]

Avoid reusing a regular expression with the g or y flag in this callback unless you deliberately reset its lastIndex: those flags make repeated calls to test() stateful and can cause alternating results.

Choose a string-matching method

Use includes() when the condition is whether a literal substring appears. Use indexOf() when you need the found position as well, remembering that -1 means no match. Use RegExp.test() for a pattern that needs regular-expression features. In each case, filter() gathers all strings whose predicate is true and preserves their source order.

If the task needs only one result, choose find() for the first matching string or findIndex() for that string’s array position. If it needs every match, choose filter(). Keep the distinction clear: includes() and RegExp.test() answer a Boolean question about one string, indexOf() reports a character position within one string, and filter() produces an array of the original matching strings.