{"record":{"id":"c13baf25f3645be9","repo":"TheAlgorithms/JavaScript","slug":"input-must-not-contain-special-characters","errorCode":null,"errorMessage":"Input must not contain special characters","messagePattern":"Input must not contain special characters","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/CountLetters.js","lineNumber":17,"sourceCode":"/**\n * @function countLetters\n * @description Given a string, count the number of each letter.\n * @param {String} str - The input string\n * @return {Object} - Object with letters and number of times\n * @example countLetters(\"hello\") => {h: 1, e: 1, l: 2, o: 1}\n */\n\nconst countLetters = (str) => {\n  const specialChars = /\\W/g\n\n  if (typeof str !== 'string') {\n    throw new TypeError('Input should be a string')\n  }\n\n  if (specialChars.test(str)) {\n    throw new TypeError('Input must not contain special characters')\n  }\n\n  if (/\\d/.test(str)) {\n    throw new TypeError('Input must not contain numbers')\n  }\n\n  const obj = {}\n  for (let i = 0; i < str.toLowerCase().length; i++) {\n    const char = str.toLowerCase().charAt(i)\n    obj[char] = (obj[char] || 0) + 1\n  }\n\n  return obj\n}\n\nexport { countLetters }\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/CountLetters.js#L1-L34","documentation":"Second guard in countLetters, fired by the regex /\\W/g. \\W matches any character that is NOT [A-Za-z0-9_], so spaces, punctuation, hyphens, accented letters, and any non-ASCII letter all throw TypeError. Note: the underscore _ is a word character and does NOT throw here. The function therefore only accepts pure [A-Za-z0-9_] input (digits are caught by the next guard).","triggerScenarios":"Calling countLetters('hello world') (space), countLetters('hi!'), countLetters('café') (é is non-word), countLetters('a-b'), countLetters('a,b').","commonSituations":"Passing free-text sentences to a function designed for single-letter token counting; expecting Unicode support when the regex is ASCII-only; forgetting that whitespace is a special character here.","solutions":["Strip non-letter characters first: str.replace(/[^A-Za-z]/g, '') before counting.","If you actually want word/char counting on prose, use a different utility (e.g. CheckWordOccurrence).","Pre-validate: if (/\\W/.test(str)) sanitize or pick another function."],"exampleFix":"// before\ncountLetters('hello world')\n\n// after\ncountLetters('hello world'.replace(/[^A-Za-z]/g, ''))","handlingStrategy":"validation","validationCode":"const clean = str.replace(/[^A-Za-z]/g, '')\ncountLetters(clean)","typeGuard":"const isLettersOnly = (s) => typeof s === 'string' && /^[A-Za-z]*$/.test(s)","tryCatchPattern":"try {\n  countLetters(str)\n} catch (e) {\n  if (e instanceof TypeError && /special characters/i.test(e.message)) {\n    countLetters(str.replace(/[^A-Za-z]/g, ''))\n  } else throw e\n}","preventionTips":["Remember \\W is ASCII [^A-Za-z0-9_] — spaces and accents throw.","Sanitize prose before letter counting.","Use a letters-only regex to pre-validate."],"tags":["string","validation","regex","letter-count","ascii-only"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}