JavaScript check if String contains at least one Number

Javascript check if string contains number example code; In this tutorial we will show you how to check if String contains at least one Number in JavaScript.

To check if a string contains at least one number in js we can use Use the RegExp.test() method, This method will return true if the string contains at least one number, otherwise false will be returned.

Example 1:

<script>
  const str1 = 'coding 32 driver';
  const str2 = 'coding driver';

  function containsNumber(str) {
    return /\d/.test(str);
  }

  console.log(containsNumber(str1)); // true
  console.log(containsNumber(str2)); // false
</script>

Example 2:

<script>
  const str1 = 'coding 32 driver';
  const str2 = 'coding driver';

  function containsNumber(str) {
    return /[0-9]/.test(str);
  }
  
  console.log(containsNumber(str1)); // true
  console.log(containsNumber(str2)); // false
</script>