October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

jQuery: Check Whether a String Contains a Specific Word

Use JavaScript includes() to find a substring, or an escaped regular expression when you need a complete word. jQuery selectors are for DOM content and attributes, not arbitrary string variables.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a plain substring check, use JavaScript’s String.prototype.includes()—jQuery is not needed. If you mean a complete, standalone word rather than characters that happen to appear inside a longer word, use a regular expression instead. The distinction matters: searching for cat with includes() also matches catalog.

Check whether a string contains a substring

includes() returns true when the search string appears anywhere in the text, and false otherwise:

const text = "The quick brown fox.";
const word = "fox";

const found = text.includes(word); // true

This is case-sensitive, so "Fox" does not match "fox". To perform a basic case-insensitive substring search, convert both strings to the same case first:

const found = text.toLowerCase().includes(word.toLowerCase());

includes() takes a string search value, not a regular expression; passing a regex causes a TypeError. Also decide how your application should handle an empty search string: text.includes("") returns true. These behaviors are documented in MDN’s JavaScript String.prototype.includes() reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check for a complete word

Use a regular expression with word-boundary assertions when you want a basic whole-word match. Escape the search term first so punctuation supplied as input is treated literally, not as regex syntax:

function escapeRegExp(value) {
  return value.replace(/[.*+?^${}()|[]\]/g, "\$&");
}

function containsWholeWord(text, word) {
  if (!word) return false;
  return new RegExp(`\b${escapeRegExp(word)}\b`, "i").test(text);
}

containsWholeWord("A fox crossed the road.", "fox"); // true
containsWholeWord("A fox crossed the road.", "Fox"); // true
containsWholeWord("A fox crossed the road.", "ox");  // false

The i flag makes this example case-insensitive. The empty-string check is an explicit choice for this helper; without it, the expression would not represent a meaningful requested word.

JavaScript’s b uses regex word-character rules, not every language’s idea of a word. In ordinary use, letters, digits, and underscores count as word characters, while punctuation such as an apostrophe can create a boundary that may not match how readers parse a word. MDN explains these word-boundary rules and notes that languages whose words are not separated by spaces, including Chinese and Thai, may need more advanced segmentation such as Intl.Segmenter. If your text has specialized tokenization or Unicode requirements, define and test the desired boundary behavior with representative examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When jQuery selectors apply

jQuery is useful for selecting elements in a document, but it is not needed to search an arbitrary JavaScript string. The jQuery [attr~="value"] selector checks for a space-delimited word in an HTML attribute; it is not a general string-search method. The jQuery selector documentation describes selectors, and the jQuery() reference covers querying DOM elements. For a standalone string variable, use includes() or a regular expression as appropriate.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.