dotnet/AspNetCore.Docs · error · Error
Syntax error, unrecognized expression: ${msg}
Error message
Syntax error, unrecognized expression: ${msg} What it means
jQuery's Sizzle selector engine throws via `Sizzle.error` when a selector string cannot be tokenized/parsed. The message is `'Syntax error, unrecognized expression: ' + msg`, surfacing the offending selector fragment.
Source
Thrown at aspnetcore/mvc/views/tag-helpers/th-components/samples/RazorPagesSample/wwwroot/lib/jquery/jquery.js:1463
}
var fn = Expr.attrHandle[ name.toLowerCase() ],
// Don't get fooled by Object.prototype properties (jQuery #13807)
val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ?
fn( elem, name, !documentIsHTML ) :
undefined;
return val !== undefined ?
val :
support.attributes || !documentIsHTML ?
elem.getAttribute( name ) :
(val = elem.getAttributeNode(name)) && val.specified ?
val.value :
null;
};
Sizzle.error = function( msg ) {
throw new Error( "Syntax error, unrecognized expression: " + msg );
};
/**
* Document sorting and removing duplicates
* @param {ArrayLike} results
*/
Sizzle.uniqueSort = function( results ) {
var elem,
duplicates = [],
j = 0,
i = 0;
// Unless we *know* we can detect duplicates, assume their presence
hasDuplicate = !support.detectDuplicates;
sortInput = !support.sortStable && results.slice( 0 );
results.sort( sortOrder );
if ( hasDuplicate ) {View on GitHub (pinned to c67a80103a)
Solutions
- Escape meta-characters in dynamic values with `$.escapeSelector(value)` before interpolation.
- Prefer attribute-value selectors with quotes: `$('[data-id="' + id + '"]')` instead of `$('#' + id)` when id may contain dots.
- Validate the selector before use, or use `.find()` / `.filter()` with a function for dynamic matching.
- Check for balanced brackets and valid syntax; log the selector to find the malformed fragment in the error message.
Example fix
// before
var $el = $('#' + userInput); // fails if userInput has '.' or ':'
// after
var $el = $('[id="' + $.escapeSelector(userInput) + '"]'); Defensive patterns
Strategy: validation
Validate before calling
function safeFind(selector, ctx) {
try { return $(selector, ctx); }
catch (e) {
if (/Syntax error/.test(e.message)) {
return ctx ? $(ctx).find(selector) : $();
}
throw e;
}
} Type guard
function isValidSelector(selector) {
try { document.createDocumentFragment().querySelector(selector); return true; }
catch (e) { return false; }
} Try / catch
try {
return $(selector);
} catch (e) {
if (/Syntax error/.test(e.message)) {
console.warn('Bad selector:', selector);
return $(); // empty collection
}
throw e;
} Prevention
- Escape dynamic values with `$.escapeSelector`.
- Use quoted attribute selectors for user-derived IDs.
- Prefer `.find()`/`.filter()` functions over string-built selectors.
When it happens
Trigger: Passing a malformed selector to any jQuery DOM method (`$()`, `.find()`, `.filter()`, `.on()` with delegation, etc.). Examples: unbalanced brackets `$('div[')`, leading/trailing special chars, invalid pseudo-classes, unescaped attribute values containing meta-characters.
Common situations: Dynamically building selectors from user input that contains `#`, `.`, `[`, `:`; attribute selectors with quotes/brackets in the value; typos in class/ID selectors; using a CSS selector where an HTML string was intended.
Related errors
- Syntax error, unrecognized expression: ${msg}
- Syntax error, unrecognized expression: ${msg}
- jQuery requires a window with a document
- Bootstrap's JavaScript requires jQuery
- Bootstrap's JavaScript requires jQuery version 1.9.1 or high
AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13).
Data as JSON: /api/errors/dfdb15bf0fd4add1.
Report an issue: GitHub.