OrchardCMS/OrchardCore · error · Error

Syntax error, unrecognized expression:

Error message

Syntax error, unrecognized expression: 

What it means

Sizzle.error is Sizzle/jQuery's way of reporting malformed selector expressions: it throws new Error('Syntax error, unrecognized expression: ' + msg). When a CSS selector string cannot be tokenized or matched by Sizzle's grammar, this error is raised from jQuery's selector engine.

Solutions

  1. Validate/interpolate the selector string before use; guard against empty values
  2. Escape special characters with CSS.escape for ids/classes containing dots or colons
  3. Use document.querySelector-compatible valid selector syntax
  4. Check the variable feeding the selector is not undefined/empty

Example fix

// before
var id = ''; $(id); // Syntax error, unrecognized expression:
// after
if (id) { $(CSS.escape ? '#' + CSS.escape(id) : '#' + id.replace(/([:.])/g, '\\$1')); }
Defensive patterns

Strategy: validation

Validate before calling

function isSafeSelector(sel) { return typeof sel === 'string' && sel.trim().length > 0 && !/[<>]|\[\s*\]|\(\s*\)/.test(sel); }

Type guard

function isNonEmptyString(v) { return typeof v === 'string' && v.trim().length > 0; }

Try / catch

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

Prevention

When it happens

Trigger: Passing an empty or syntactically invalid selector to $()/jQuery(), e.g. $(''), $('#'), $('div['), or a selector containing unescaped special characters; also find()/filter()/is() with invalid expressions.

Common situations: Dynamically built selectors from empty variables or user input; attribute selectors with unquoted values; selectors containing unescaped dots in IDs (e.g. from .NET names like asp-for controls); templates where a variable is empty at render time.

Related errors


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

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Resources/wwwroot/Vendor/jquery-3.4.1/jquery.js:1560

		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)