The attribute contains prefix selector matches elements whose attribute is exactlyvalue, or starts with value followed by a hyphen (-). Available since jQuery 1.0 (standard CSS).
Remember
$("[attribute|='value']")
$("a[hreflang|='en']")
✓ hreflang="en"
✓ hreflang="en-US"
✗ hreflang="english" (no hyphen after en)
✗ hreflang="fr"
[attr|="en"] ≠ [attr^="en"] // ^= also matches "english"
Remember: longer matches need a hyphen after the prefix (en-US, not enUS).
Do not use |= when you only want a raw string start — use ^= instead.
Piece
Required?
Role
attribute
Yes
Attribute name to test
value
Yes
Exact match, or prefix before a hyphen
Quotes around value
Recommended
Required for many values; identifiers may omit them
Pseudocode
Pseudocode
attrName = given attribute
prefix = given value
matches = empty list
for each element with attrName:
v = element.getAttribute(attrName)
if v == prefix:
add element // exact
else if v starts with (prefix + "-"):
add element // hyphen prefix
return jQuery(matches)
Reference
Cheat Sheet
Keep these facts handy while matching hyphenated attribute values.
Topic
Detail
Syntax
$("[attr|='value']")
Matches
value or value-*
Classic use
$("a[hreflang|='en']")
vs =
= is exact only
vs ^=
^= is any start (no hyphen rule)
vs ~=
~= is a whole word in a space list
Since
jQuery 1.0 (standard CSS)
Try it
Live Preview
Run prefix / starts-with / equals selectors against sample hreflang links and see which values match.
Sample markup mirrors the official jQuery hreflang demo, plus an english trap value.
Limit to anchors. The a tag keeps the search on links only.
Apply |=. Values must be en or start with en-.
Collect attributes..map() prints each matching hreflang.
Official pattern. Same idea as the jQuery API demo that outlines English links.
Example 2 — |= vs ^=
See why english matches starts-with but not the hyphen prefix selector.
jQuery
console.log("|= :", $("a[hreflang|='en']").length);
console.log("^= :", $("a[hreflang^='en']").length);
// |= skips hreflang="english"
// ^= includes "english" because it starts with "en"
2, 2-beta
(20 and 2beta are skipped — no hyphen after 2)
How It Works
Prefix token.2 matches exactly or as 2-….
Hyphen required.2beta and 20 fail the rule.
List values. Mapping data-version shows which tokens passed.
Design tip. Structure attribute values with hyphens if you plan to use |=.
Edge Cases & Pitfalls
Check these before relying on |= in real projects.
trap
|= ≠ ^=
[hreflang|="en"] skips english. [hreflang^="en"] includes it. Pick the operator that matches your intent.
hyphen
No hyphen, no extended match
enUS or 2beta do not match a prefix of en / 2. The suffix must follow a hyphen.
missing
Missing attribute never matches
An element without the attribute is skipped — even if nearby siblings match.
case
Matching is case-sensitive in HTML attributes
In HTML documents, attribute selector matching is generally ASCII case-sensitive for values. Prefer consistent lowercase language tags.
Compatibility
Browser Support
The attribute contains prefix selector [attr|="value"] is standard CSS and has worked in jQuery since 1.0. Modern browsers implement it in native querySelectorAll.
✓ CSS · jQuery 1.0+
jQuery [name|="value"]
Supported in all modern browsers. Native: document.querySelectorAll("a[hreflang|='en']"). Designed for language-style hyphenated values.
100%Standard CSS selector
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
|= combinatorUniversal
Bottom line: Safe everywhere jQuery runs. Use for language tags and other hyphen-prefixed tokens; switch to ^= when you need a plain string prefix.
Analysis
Performance Notes
Approach
Cost
Best for
$("a[hreflang|='en']")
Native-friendly CSS
Tagged links / scoped queries
$("[lang|='en']")
Wider scan
Any element with lang
$("[attr^='en']")
Similar
Loose prefixes (not language rules)
Attribute selectors are standard CSS, so jQuery can often delegate to the browser engine. Narrow with a tag (a[…]) or a container ($("#nav").find("a[hreflang|='en']")) on large pages.
Remember
Key Takeaways
Role: match exact value or value-* hyphen prefixes.
Classic use: language tags like en / en-US.
Not ^=:english matches ^=, not |=.
Standard CSS: works in jQuery 1.0+ and native querySelectorAll.
One line: use [attr|="value"] for exact or hyphen-prefixed values — ideal for language codes.
Frequently Asked Questions
It selects elements whose named attribute equals value exactly, or whose value begins with value followed immediately by a hyphen (-). For example, [hreflang|="en"] matches hreflang="en" and hreflang="en-US", but not hreflang="english".
The CSS specification introduced it primarily for language attributes — BCP 47 tags like en, en-US, and en-GB share a common language prefix separated by hyphens.
[attr^="en"] matches any value starting with the letters en — including "english" or "enable". [attr|="en"] is stricter: only "en" or "en-something" (hyphen after en) match.
Yes. Attribute selectors including the contains-prefix form have worked in jQuery since 1.0. Modern browsers also support it in native querySelectorAll.
Yes for extended matches. The prefix alone matches exactly. A longer match requires value + hyphen + suffix. "enUS" without a hyphen does not match [hreflang|="en"].
Yes. $("a[hreflang|=\"en\"]") limits results to anchors with matching hreflang — the pattern used in the official jQuery demo.