Check if String contains at least one Number in JavaScript-Code Examples

Radu TM

Check if String contains at least one Number in JavaScript

JavaScript by Radu TM June 22, 2022

const containsNumber = (str) => {
  // Use a regex to check if string contains at least one number
  const regex = /\d/;
  return regex.test(str);
}

// Test
console.log(containsNumber("1")); // true
console.log(containsNumber("I'm 23 years old")); // true
console.log(containsNumber("I'm a string")); // false

0

28.752

Check if String contains at least one Number in JavaScript

JavaScript by Radu TM June 22, 2022

const containsNumber = (str) => {
  // Use a regex to check if string contains at least one number
  const regex = /\d/;
  return regex.test(str);
}

// Test our function
console.log(containsNumber('abc')); // false
console.log(containsNumber('abc123')); // true

0

28.752

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories