Hướng dẫn how do you check if a character in a string is a number in javascript? - làm thế nào để bạn kiểm tra xem một ký tự trong một chuỗi có phải là một số trong javascript hay không?

var isIntegerTest = /^\d+$/;
var isDigitArray = [!0, !0, !0, !0, !0, !0, !0, !0, !0, !0];

function hasLeading0s(s) {
  return !(typeof s !== 'string' ||
    s.length < 2 ||
    s[0] !== '0' ||
    !isDigitArray[s[1]] ||
    isIntegerTest.test(s));
}
var isWhiteSpaceTest = /\s/;

function fIsNaN(n) {
  return !(n <= 0) && !(n > 0);
}

function isNumber(s) {
  var t = typeof s;
  if (t === 'number') {
    return (s <= 0) || (s > 0);
  } else if (t === 'string') {
    var n = +s;
    return !(fIsNaN(n) || hasLeading0s(s) || !(n !== 0 || !(s === '' || isWhiteSpaceTest.test(s))));
  } else if (t === 'object') {
    return !(!(s instanceof Number) || fIsNaN(+s));
  }
  return false;
}

function testRunner(IsNumeric) {
  var total = 0;
  var passed = 0;
  var failedTests = [];

  function test(value, result) {
    total++;
    if (IsNumeric(value) === result) {
      passed++;
    } else {
      failedTests.push({
        value: value,
        expected: result
      });
    }
  }
  // true
  test(0, true);
  test(1, true);
  test(-1, true);
  test(Infinity, true);
  test('Infinity', true);
  test(-Infinity, true);
  test('-Infinity', true);
  test(1.1, true);
  test(-0.12e-34, true);
  test(8e5, true);
  test('1', true);
  test('0', true);
  test('-1', true);
  test('1.1', true);
  test('11.112', true);
  test('.1', true);
  test('.12e34', true);
  test('-.12e34', true);
  test('.12e-34', true);
  test('-.12e-34', true);
  test('8e5', true);
  test('0x89f', true);
  test('00', true);
  test('01', true);
  test('10', true);
  test('0e1', true);
  test('0e01', true);
  test('.0', true);
  test('0.', true);
  test('.0e1', true);
  test('0.e1', true);
  test('0.e00', true);
  test('0xf', true);
  test('0Xf', true);
  test(Date.now(), true);
  test(new Number(0), true);
  test(new Number(1e3), true);
  test(new Number(0.1234), true);
  test(new Number(Infinity), true);
  test(new Number(-Infinity), true);
  // false
  test('', false);
  test(' ', false);
  test(false, false);
  test('false', false);
  test(true, false);
  test('true', false);
  test('99,999', false);
  test('#abcdef', false);
  test('1.2.3', false);
  test('blah', false);
  test('\t\t', false);
  test('\n\r', false);
  test('\r', false);
  test(NaN, false);
  test('NaN', false);
  test(null, false);
  test('null', false);
  test(new Date(), false);
  test({}, false);
  test([], false);
  test(new Int8Array(), false);
  test(new Uint8Array(), false);
  test(new Uint8ClampedArray(), false);
  test(new Int16Array(), false);
  test(new Uint16Array(), false);
  test(new Int32Array(), false);
  test(new Uint32Array(), false);
  test(new BigInt64Array(), false);
  test(new BigUint64Array(), false);
  test(new Float32Array(), false);
  test(new Float64Array(), false);
  test('.e0', false);
  test('.', false);
  test('00e1', false);
  test('01e1', false);
  test('00.0', false);
  test('01.05', false);
  test('00x0', false);
  test(new Number(NaN), false);
  test(new Number('abc'), false);
  console.log('Passed ' + passed + ' of ' + total + ' tests.');
  if (failedTests.length > 0) console.log({
    failedTests: failedTests
  });
}
testRunner(isNumber)

Để kiểm tra xem một chuỗi có chứa các số trong JavaScript, hãy gọi phương thức test() trên Regex: /\d/ này. test() sẽ trả về

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
1 nếu chuỗi chứa số. Nếu không, nó sẽ trả lại
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
2.

Ví dụ:

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

Phương thức kiểm tra regexp () tìm kiếm sự phù hợp giữa biểu thức chính quy và chuỗi.

Các ký tự

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
3 và
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
3 được sử dụng để bắt đầu và kết thúc biểu thức chính quy.

Metacharacter

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5 khớp với bất kỳ chữ số nào (
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
6 -
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
7) trong chuỗi.

Bạn cũng có thể sử dụng

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
8 để phù hợp với các chữ số. Mẫu này phù hợp với bất kỳ ký tự số nào giữa
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
6 và
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
7.

function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

Bạn có thể thấy

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
8 dễ đọc hơn là sử dụng
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5, đặc biệt nếu bạn không quen thuộc lắm với các nhân vật đặc biệt trong các biểu thức thông thường.

Nền tảng đám mây đơn giản nhất cho các nhà phát triển & nhóm. Bắt đầu với một khoản tín dụng miễn phí $ 100.$100 free credit.

Kiểm tra xem chuỗi chỉ chứa các số

Để kiểm tra xem chuỗi chỉ chứa các số, chúng tôi sẽ phải sử dụng một biểu thức thông thường khác -

function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
3:only numbers, we’ll have to use a different regular expression –
function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
3:

function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false

Ký tự

function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
4 đánh dấu sự khởi đầu của đầu vào chuỗi và ký tự
function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5 đánh dấu sự kết thúc của nó.

Thêm ký tự

function containsNumbers(str) {
  return /[0-9]/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
6 sau khi
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5 làm cho regex khớp với một hoặc nhiều lần xuất hiện của mẫu
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5.

Vì vậy, Regex khớp với một chuỗi bắt đầu và kết thúc với một chuỗi các chữ số liên tiếp.

Như chúng tôi đã làm trước đây, chúng tôi có thể thay thế

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

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
5 bằng
function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
8 ở đây:

function containsOnlyNumbers(str) {
  return /^[0-9]+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false

Phương thức chuỗi khớp ()

Chúng ta có thể sử dụng phương thức

function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
1
function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
2 thay cho
function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
3 test() để kiểm tra xem chuỗi có chứa các số

function containsNumbers(str) {
  return Boolean(str.match(/\d/));
}

console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true

Phương thức Trận đấu chuỗi () trả về một mảng của tất cả các kết quả phù hợp của biểu thức chính quy trong một chuỗi. Nếu không có trận đấu, nó sẽ trả về

function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
5.

function containsNumbers(str) {
  return str.match(/\d/);
}

console.log(containsNumbers('hello123'));
// [ '1', index: 5, input: 'hello123', groups: undefined ]

console.log(containsNumbers('javascript')); // null

console.log(containsNumbers('3 apples'));
// [ '3', index: 0, input: '3 apples', groups: undefined ]

Chúng tôi chuyển kết quả của

function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
2 cho hàm tạo
function containsOnlyNumbers(str) {
  return /^\d+$/.test(str);
}

console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
7 để chuyển đổi nó thành A & nbsp; ________ 38 giá trị. & NBSP;

Trong JavaScript, có sáu giá trị giả: & nbsp; ________ 42, & nbsp; ________ 35, & nbsp; ____ 44, & nbsp; Mọi giá trị khác là sự thật.

console.log(Boolean(undefined)); // false
console.log(Boolean(['number60'])); // true
console.log(Boolean(null)); // false
console.log(Boolean(5)); // true

11 Tính năng JavaScript mới tuyệt vời trong ES13

Hướng dẫn này sẽ đưa bạn lên tốc độ với tất cả các tính năng mới nhất được thêm vào Ecmascript 13. Các tính năng mới mạnh mẽ này sẽ hiện đại hóa JavaScript của bạn với mã ngắn hơn và biểu cảm hơn.

Hướng dẫn how do you check if a character in a string is a number in javascript? - làm thế nào để bạn kiểm tra xem một ký tự trong một chuỗi có phải là một số trong javascript hay không?

Đăng ký và nhận một bản sao miễn phí ngay lập tức.

Hướng dẫn how do you check if a character in a string is a number in javascript? - làm thế nào để bạn kiểm tra xem một ký tự trong một chuỗi có phải là một số trong javascript hay không?

Ayibatari Ibaba là một nhà phát triển phần mềm với nhiều năm kinh nghiệm xây dựng các trang web và ứng dụng. Ông đã viết nhiều về một loạt các chủ đề lập trình và đã tạo ra hàng chục ứng dụng và thư viện nguồn mở.

Làm thế nào để bạn kiểm tra xem một ký tự chuỗi là một số?

Chúng ta có thể kiểm tra xem ký tự đã cho trong chuỗi là số/chữ cái bằng cách sử dụng phương thức isDigit () của lớp ký tự. Phương thức isDigit () là một phương thức tĩnh và xác định xem ký tự được chỉ định là một chữ số.using isDigit() method of Character class. The isDigit() method is a static method and determines if the specified character is a digit.

Làm thế nào để bạn kiểm tra xem một ký tự là một chữ cái hoặc số trong javascript?

Để kiểm tra xem ký tự là chữ cái, hãy gọi phương thức Test () trên biểu thức chính quy sau- /^[A-Za-Z]+$ /.Nếu ký tự là một chữ cái, phương thức kiểm tra sẽ trả về true, nếu không sẽ được trả về.call the test() method on the following regular expression - /^[a-zA-Z]+$/ . If the character is a letter, the test method will return true , otherwise false will be returned.

Làm thế nào để bạn kiểm tra xem một ký tự không phải là một số trong JS?

Trong JavaScript, có hai cách để kiểm tra xem một biến có phải là số không: isnan () - là viết tắt của không phải là một số, nếu biến không phải là số, nó trả về đúng, nếu không trả về sai.Loại - nếu biến là một số, nó sẽ trả về một chuỗi có tên là Số Số.isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

Làm thế nào để bạn kiểm tra xem một chuỗi có chứa giá trị trong javascript không?

Phương thức bao gồm () trả về true nếu một chuỗi chứa một chuỗi được chỉ định.Nếu không thì nó trả về sai.Phương pháp bao gồm () là trường hợp nhạy cảm. returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.