OrchardCMS/OrchardCore · error · Error

Syntax error, unrecognized expression:

Error message

Syntax error, unrecognized expression: 

What it means

The slim build bundles the same Sizzle selector engine, so it throws the identical 'Syntax error, unrecognized expression' when a selector string cannot be parsed or uses unsupported pseudo-selectors. The offending selector text is appended to the message.

Solutions

  1. Extract the selector text from the message after 'Syntax error, unrecognized expression: ' and validate/escape it at the call site.
  2. Replace string-interpolated selectors with .filter(fn) or attribute-comparison helpers, or escape special chars using jQuery.escapeSelector / Sizzle.escape.
  3. Register missing custom pseudo-selectors via $.expr.pseudos, or use the full (non-slim) build if a plugin requires it.
  4. Prefer passing DOM node references to $() instead of composed selector strings for dynamic values.

Example fix

// before
$('.' + rawClassName); // throws if rawClassName has spaces/brackets
// after
$(document.querySelectorAll('[class]')).filter(function(){
  return this.classList.contains(rawClassName);
});
Defensive patterns

Strategy: validation

Validate before calling

function safeSelect(sel) {
  try { document.querySelector(sel); return true; }
  catch (e) { return false; }
}
if (safeSelect(mySelector)) { $(mySelector); } else { console.warn('Invalid selector skipped:', mySelector); }

Type guard

function isPlainSelector(v) { return typeof v === 'string' && !/[\u0000-\u001f]/.test(v) && v.trim().length > 0; }

Try / catch

try { $el = $(sel); } catch (e) { if (String(e.message).includes('unrecognized expression')) { $el = $(); console.warn('Rejected selector', sel); } else { throw e; } }

Prevention

When it happens

Trigger: $(expr)/find(expr)/closest(expr) with malformed strings — unbalanced quotes, brackets or parens — or unknown pseudo-classes; passing a value jQuery cannot coerce into a selector, element, or array-like.

Common situations: Interpolating user input into selector strings; typos or removed plugin pseudo-selectors; selectors generated from configuration or attributes containing special characters like . # : [ ].

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/a1f32c960fdfa7f2. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Resources/wwwroot/Vendor/jquery-3.5.1/jquery.slim.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)