{"record":{"id":"1fc835b29508c5ef","repo":"dotnet/AspNetCore.Docs","slug":"syntax-error-unrecognized-expression-msg","errorCode":null,"errorMessage":"Syntax error, unrecognized expression: ${msg}","messagePattern":"Syntax error, unrecognized expression: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/jquery-2.2.0.js","lineNumber":1463,"sourceCode":"        }\n    \n        var fn = Expr.attrHandle[ name.toLowerCase() ],\n            // Don't get fooled by Object.prototype properties (jQuery #13807)\n            val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ?\n                fn( elem, name, !documentIsHTML ) :\n                undefined;\n    \n        return val !== undefined ?\n            val :\n            support.attributes || !documentIsHTML ?\n                elem.getAttribute( name ) :\n                (val = elem.getAttributeNode(name)) && val.specified ?\n                    val.value :\n                    null;\n    };\n    \n    Sizzle.error = function( msg ) {\n        throw new Error( \"Syntax error, unrecognized expression: \" + msg );\n    };\n    \n    /**\n     * Document sorting and removing duplicates\n     * @param {ArrayLike} results\n     */\n    Sizzle.uniqueSort = function( results ) {\n        var elem,\n            duplicates = [],\n            j = 0,\n            i = 0;\n    \n        // Unless we *know* we can detect duplicates, assume their presence\n        hasDuplicate = !support.detectDuplicates;\n        sortInput = !support.sortStable && results.slice( 0 );\n        results.sort( sortOrder );\n    \n        if ( hasDuplicate ) {","sourceCodeStart":1445,"sourceCodeEnd":1481,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/jquery-2.2.0.js#L1445-L1481","documentation":"jQuery 2.x uses Sizzle as its selector engine. Sizzle.error is the single choke point for any unparseable selector passed to $(), .find(), .filter(), .children(), etc. The thrown message appends the offending expression so you can see what failed. HTML strings that look like selectors and selectors with unescaped metacharacters both end here.","triggerScenarios":"Calling $('#a.b') (dot treated as class), $('.foo:bar'), $('<div') malformed HTML parsed as selector, $(null) in some paths, or interpolating user input directly into a selector.","commonSituations":"Element IDs containing dots/colons/brackets; dynamically built selectors from data attributes; copy-pasting a CSS selector that uses pseudo-classes Sizzle doesn't support.","solutions":["Escape metacharacters with $.escapeSelector (jQuery 3+) or a manual escape: id.replace(/([.:#\\[\\],])/g, '\\\\$1').","Prefer document.getElementById and wrap with $(): $(document.getElementById('a.b.c')).","If you meant HTML, make it a valid complete tag string.","Sanitize/validate any user-supplied string before using it as a selector."],"exampleFix":"// before\n$('.price[special]'); // or $('#user.name'); // throws on weird ids\n// after\n$('#' + $.escapeSelector('user.name'));\n// or\n$(document.getElementById('user.name'));","handlingStrategy":"validation","validationCode":"// Escape metacharacters before building selectors from dynamic ids.\nfunction safeIdSelector(id) {\n  return '#' + (window.jQuery && jQuery.escapeSelector ? jQuery.escapeSelector(id) : id.replace(/([!\\\"#$%&'()*+,./:;<=>?@[\\]^`{|}~])/g, '\\\\$1'));\n}\n$(safeIdSelector(dynamicId)).hide();","typeGuard":"function looksLikeValidSelector(s) {\n  if (typeof s !== 'string' || s.length === 0) return false;\n  try { $(s); return true; } catch (e) { return false; }\n}","tryCatchPattern":"try {\n  return $(userSelector);\n} catch (e) {\n  if (/Syntax error, unrecognized expression/.test(e.message)) {\n    console.warn('Invalid selector, ignoring:', userSelector);\n    return $(); // empty collection\n  }\n  throw e;\n}","preventionTips":["Prefer document.getElementById for known ids; wrap the result with $().","Never interpolate raw user input into a selector.","Use $.escapeSelector for ids/classes containing metacharacters."],"tags":["jquery","sizzle","selector","css","escaping"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}