HOWTO · TypeScript

How to Check if a String Has a Certain Text in TypeScript

Learn how to check whether a string contains text in TypeScript with includes(), indexOf(), search(), and RegExp.test(). Compare their return values and handle case sensitivity and multiple search terms.

On this page

Use includes() to check directly whether a TypeScript string contains a substring; it returns a Boolean. Use indexOf() when you also need the match position, and use search() or RegExp.test() when the condition is a regular-expression pattern. These methods are case-sensitive unless you explicitly normalize the text or use a case-insensitive pattern.

The executable snippets use JavaScript syntax that is also valid TypeScript, so you can paste them into a TypeScript file without a transpilation-specific setup.

Use includes() for a direct substring check

Use includes() for a direct Boolean answer: it returns true when the searched string contains the supplied substring and false otherwise. For a simple substring check, this readable Boolean result is usually the right choice.

For a direct Boolean answer, call includes() on the string you want to search. The argument is the candidate text.

const message = "TypeScript string search";
const query = "string";
const containsQuery = message.includes(query);

console.log(containsQuery);
true

The result is true because the exact lowercase substring occurs in message. For example, searching for "String" instead would return false; includes() does not change capitalization or perform locale-aware comparison. If capitalization should not matter for your data, normalize both values before comparing, as shown in the final section.

Use indexOf() when you need the match position

indexOf() searches the receiver for the first occurrence of a substring and returns its zero-based position. It returns -1 when there is no match. To convert that result to a Boolean, compare it explicitly with -1: checking the number directly in an if condition would mishandle a match at position 0, because zero is falsy.

const message = "TypeScript string search";
const query = "TypeScript";
const position = message.indexOf(query);
const containsQuery = position !== -1;

console.log(position, containsQuery);
0 true

Here the first character is at position zero, so the comparison correctly reports a match. Use the position when you need to highlight or slice around the first occurrence; use includes() when you only need presence. Both methods compare literal text and are case-sensitive. An empty search string is considered present by these string methods, so validate user input separately if a blank query should not count as a match.

Use search() for regular-expression matches

search() returns the position of the first regular-expression match, or -1 if it finds none. Passing a RegExp value makes the pattern explicit, as in the example below. JavaScript also converts a string argument to a regular expression, so a string passed to search() is still pattern syntax; it does not search for literal text the way includes() does. Its numeric return value follows the same -1 comparison rule as indexOf().

const message = "Order 42 is ready";
const position = message.search(/\d+/);

console.log(position);
6

The expression /\d+/ matches one or more digits, and search() gives the starting position of that match. For example, "42" passed as a string is converted to a regular expression and matches those digits as a pattern. If you only need a yes-or-no pattern result, RegExp.test() expresses that intent more directly.

Use RegExp.test() for pattern checks

Call test() on a regular expression to get a Boolean indicating whether the input matches. A regular expression literal is a safe, concise choice when the pattern is part of your code. Regular expressions can express character classes, repetition, and alternatives that a literal substring search cannot.

const message = "TypeScript string search";
const pattern = /TypeScript/;
const matches = pattern.test(message);

console.log(matches);
true

When a pattern comes from a variable, constructing a RegExp treats characters such as ., *, and [ as pattern syntax. If the variable is meant to be literal text, escape those characters first:

const userText = "price (USD)";
const escapedText = userText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const literalPattern = new RegExp(escapedText);
const found = literalPattern.test("The price (USD) is listed");

console.log(found);
true

This prevents punctuation in userText from changing the intended literal search. Avoid the g and y flags when repeatedly using test() as a simple Boolean check: those flags make matching depend on the regular expression’s mutable lastIndex state.

Handle case sensitivity and multiple search terms

includes(), indexOf(), and regular expressions are case-sensitive unless you choose a different comparison. For simple case-insensitive substring matching, normalize both strings the same way. toLowerCase() is suitable for many programming identifiers and English text; it is not a universal substitute for locale-aware linguistic comparison.

const message = "TypeScript makes text checks clear";
const query = "TYPESCRIPT";
const containsQuery = message.toLowerCase().includes(query.toLowerCase());

const candidates = ["JavaScript", "TypeScript", "Python"];
const containsCandidate = candidates.some((item) =>
  message.includes(item)
);

console.log(containsQuery, containsCandidate);
true true

The first check ignores capitalization by normalizing both sides. The some() call checks whether at least one candidate occurs in the message; it stops once it finds a match. Use every() instead when all listed terms must be present. Decide explicitly whether blank search terms should be accepted before using these patterns, since a blank substring matches every string. Choose includes() for literal presence, indexOf() when the position matters, and search() or RegExp.test() when the requirement is a pattern.