OrchardCMS/OrchardCore · error · Error

Syntax error, unrecognized expression:

Error message

Syntax error, unrecognized expression: 

What it means

Sizzle, jQuery's selector engine, throws this when a selector expression cannot be parsed or is not supported. jQuery.typed access like $('#id'), $('.class') is fine, but malformed strings (unbalanced brackets/quotes) or unsupported pseudo-selectors reach Sizzle.error. The thrown message appends the offending selector text.

Solutions

  1. Log the selector at the throw site: the text after 'Syntax error, unrecognized expression: ' identifies the bad input; validate/escape it before passing to $().
  2. Sanitize or reject user-supplied fragments before composing selectors (escape quotes and brackets, or use explicit APIs like .filter(fn) instead of dynamic selector strings).
  3. Replace unsupported custom pseudo-selectors with jQuery plugin definitions ($.expr.pseudos) or plain filter functions.
  4. Prefer programmatic APIs (e.g. document.getElementById result passed into $(node)) over string interpolation when values come from user input.

Example fix

// before
$("[data-x='" + userInput + "']"); // throws if userInput contains ' or ]
// after
$("[data-x]").filter(function(){ return $(this).data('x') === userInput; });
Defensive patterns

Strategy: validation

Validate before calling

function isValidSelector(sel) {
  if (typeof sel !== 'string' || !sel.trim()) return false;
  try { document.createDocumentFragment().querySelector(sel); } catch (e) { return false; }
  return true;
}
// call $(sel) only if isValidSelector(sel)

Type guard

function isSelectorString(v) { return typeof v === 'string' && /^[\w\s.#>\-\[\]:()"'=,~+*|^$@]*$/.test(v); }

Try / catch

try { $(sel); } catch (e) { if (e.message.startsWith('Syntax error, unrecognized expression')) { console.error('Bad selector:', sel); return $(); } throw e; }

Prevention

When it happens

Trigger: Calling $(expr) / find(expr) / closest(expr) with a syntactically invalid selector (e.g. 'div[', "a[href='x]"), an unknown custom pseudo-class like :mywidget, or passing a non-string non-element object that jQuery cannot interpret as a selector.

Common situations: Building selectors by string concatenation and injecting unescaped user data with quotes/brackets; typos in custom :pseudo names after plugins were removed; using selectors valid in querySelectorAll polyfills but not in Sizzle's grammar; dynamically generated selectors from config values.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/78d89bd182921fcb. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Resources/wwwroot/Vendor/jquery-3.5.1/jquery.js:1677

		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.escape = function( sel ) {
	return ( sel + "" ).replace( rcssescape, fcssescape );
};

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 4306c0717f)