Skip to content

Instantly share code, notes, and snippets.

@petergi
Created February 25, 2026 19:10
Show Gist options
  • Select an option

  • Save petergi/710988bc9cb897a138ae13526d32fd34 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/710988bc9cb897a138ae13526d32fd34 to your computer and use it in GitHub Desktop.
Checks if a string contains only alphanumeric characters. - Use `RegExp.prototype.test()` to check if the input string matches against the alphanumeric regexp pattern.
// Checks if a string contains only alphanumeric characters.
//
// - Use `RegExp.prototype.test()` to check if the input string matches against the alphanumeric regexp pattern.
const isAlphaNumeric = str => /^[a-z0-9]+$/gi.test(str);
isAlphaNumeric('hello123'); // true
isAlphaNumeric('123'); // true
isAlphaNumeric('hello 123'); // false (space character is not alphanumeric)
isAlphaNumeric('#$hello'); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment