{"record":{"id":"dc52b15856698cc6","repo":"JamesNK/Newtonsoft.Json","slug":"path-ended-with-an-open-regex","errorCode":null,"errorMessage":"Path ended with an open regex.","messagePattern":"Path ended with an open regex\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs","lineNumber":750,"sourceCode":"                        if (char.IsLetter(currentChar))\n                        {\n                            _currentIndex++;\n                        }\n                        else\n                        {\n                            break;\n                        }\n                    }\n\n                    return _expression.Substring(startIndex, _currentIndex - startIndex);\n                }\n                else\n                {\n                    _currentIndex++;\n                }\n            }\n\n            throw new JsonException(\"Path ended with an open regex.\");\n        }\n\n        private bool Match(string s)\n        {\n            int currentPosition = _currentIndex;\n            for (int i = 0; i < s.Length; i++)\n            {\n                if (currentPosition < _expression.Length && _expression[currentPosition] == s[i])\n                {\n                    currentPosition++;\n                }\n                else\n                {\n                    return false;\n                }\n            }\n\n            _currentIndex = currentPosition;","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs#L732-L768","documentation":"Thrown by ReadRegexString when a JSONPath regex literal (/pattern/flags) is opened with '/' but the closing '/' is never found before end-of-input. The regex was started (e.g. via =~ operator) but never terminated.","triggerScenarios":"A query using the =~ regex operator whose pattern is never closed. Example: token.SelectTokens(\"$..[?(@.name =~ /bob\") — the regex opens but no second '/' appears, so the parser runs to the end of the string.","commonSituations":"Writing a regex query and forgetting the closing slash; constructing regex from user input that itself contains slashes consumed as part of the pattern; assuming flags must come before the closing slash.","solutions":["Terminate the regex with a second '/' and optional trailing flags (letters).","Escape literal '/' inside the pattern as \\\\/ so it is not treated as the terminator.","Test the regex query against a known token in a unit test."],"exampleFix":"// before\ntoken.SelectTokens(\"$..[?(@.name =~ /bob\");\n// after\ntoken.SelectTokens(\"$..[?(@.name =~ /bob/i]\");","handlingStrategy":"validation","validationCode":"static bool RegexClosed(string path)\n{\n    bool inRegex = false;\n    for (int i = 0; i < path.Length; i++)\n    {\n        if (path[i] == '/' && (i == 0 || path[i-1] != '\\\\')) inRegex = !inRegex;\n    }\n    return !inRegex;\n}","typeGuard":null,"tryCatchPattern":"try { token.SelectTokens(path).ToList(); }\ncatch (JsonException ex) when (ex.Message == \"Path ended with an open regex.\")\n{ /* regex literal missing closing slash */ }","preventionTips":["Always terminate regex literals with a second '/'.","Escape literal '/' as \\\\/ inside the pattern."],"tags":["jsonpath","json","parsing","regex","jpath"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}