/**
 * escapeRegex
 *
 * Escapes characters that carry special meaning inside a regular expression so
 * that arbitrary user input can be safely used in a MongoDB `$regex` query.
 *
 * Without escaping, a user typing characters like `(`, `*`, `[`, or `\` in a
 * search box produces an invalid regular expression, which makes MongoDB throw
 * (e.g. "Regular expression is invalid: missing closing parenthesis") and the
 * request fails with a 500. Escaping turns every special character into a
 * literal, so the search matches the text the user actually typed.
 *
 * This mirrors the inline pattern already used in auth.ts / companies.ts and is
 * the single shared helper new code should use for regex-based search.
 */
export function escapeRegex(str: string): string {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
