{"record":{"id":"5b350d137a7a1d0d","repo":"sveltejs/kit","slug":"regular-expressions-are-not-valid-remote-function","errorCode":null,"errorMessage":"Regular expressions are not valid remote function arguments","messagePattern":"Regular expressions are not valid remote function arguments","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/shared.js","lineNumber":102,"sourceCode":"// \"sveltekit remote arg\"\nconst remote_object = '__skrao';\nconst remote_map = '__skram';\nconst remote_set = '__skras';\nconst remote_file = '__skraf';\nconst remote_promise_guard = '__skrap';\nconst remote_regex_guard = '__skrag';\nconst remote_arg_marker = Symbol(remote_object);\n\n/**\n * @param {boolean} sort\n */\nfunction create_remote_arg_reducers(sort) {\n\t/** @type {Record<string, (value: unknown) => unknown>} */\n\tconst remote_fns_reducers = {\n\t\t/** @param {unknown} value */\n\t\t[remote_regex_guard]: (value) => {\n\t\t\tif (value instanceof RegExp) {\n\t\t\t\tthrow new Error('Regular expressions are not valid remote function arguments');\n\t\t\t}\n\t\t}\n\t};\n\n\tif (sort) {\n\t\tconst clones = new Map();\n\n\t\t/** @type {(value: unknown) => Array<[unknown, unknown]> | undefined} */\n\t\tremote_fns_reducers[remote_map] = (value) => {\n\t\t\tif (!(value instanceof Map)) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t/** @type {Array<[string, string]>} */\n\t\t\tconst entries = [];\n\n\t\t\tfor (const [key, val] of value) {\n\t\t\t\tentries.push([stringify(key), stringify(val)]);","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/shared.js#L84-L120","documentation":"Remote functions serialize their arguments to send to the server. RegExp is not JSON-serializable and can't be safely reconstructed, so a guard reducer throws when any RegExp appears in the arguments.","triggerScenarios":"Calling a remote function (e.g. 'use server' query/command) with an argument that is a RegExp or contains one, like searchRegex passed as a filter.","commonSituations":"Reusing client-side search code that builds RegExp from user input and passing it to the remote function instead of the raw pattern string.","solutions":["Pass the pattern string and flags separately and construct the RegExp on the server","Serialize as a plain object: { source: re.source, flags: re.flags } and rebuild remotely","Strip RegExp values from your arguments before invoking the remote function"],"exampleFix":"// before\nconst results = await search(new RegExp(term, 'i'));\n// after\nconst results = await search({ source: term, flags: 'i' }); // server: new RegExp(source, flags)","handlingStrategy":"validation","validationCode":"function assertNoRegExp(value, seen = new Set()) {\n  if (value instanceof RegExp) throw new Error('RegExp found in remote args');\n  if (value && typeof value === 'object' && !seen.has(value)) {\n    seen.add(value);\n    Object.values(value).forEach((v) => assertNoRegExp(v, seen));\n  }\n}\n// call on arguments before invoking remote function","typeGuard":"function isRegExp(v) { return v instanceof RegExp; }\n// filter: if (args.some(isRegExp)) rebuild args as { source, flags };","tryCatchPattern":"try {\n  await remoteSearch(args);\n} catch (e) {\n  if (e.message.includes('Regular expressions are not valid')) {\n    console.error('Convert RegExp args to { source, flags }');\n  }\n}","preventionTips":["Never pass RegExp to remote functions — pass pattern strings","Sanitize argument objects for RegExp before invoking","Document expected arg shapes for remote functions"],"tags":["remote-functions","serialization","validation"],"backgroundTag":"invalid-remote-function-argument","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}