Skip to content

Instantly share code, notes, and snippets.

@LeanSeverino1022
Last active April 3, 2021 08:09
Show Gist options
  • Save LeanSeverino1022/c7b7042d2558edf98ee78bb502d28ddd to your computer and use it in GitHub Desktop.
Save LeanSeverino1022/c7b7042d2558edf98ee78bb502d28ddd to your computer and use it in GitHub Desktop.
check if a string contains digits js #strings #vanillajs

make use of RegExp.prototype.test()

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

function hasNumber(myString) {
  return /\d/.test(myString);
}
/\d/.test("dfgsdswefx"); // false

/\d/.test("df3sdswef"); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment