javascript,判断 val 是否为单个数字的最佳方法

发布于 2024-09-26 06:21:55 字数 216 浏览 0 评论 0原文

判断 javascript 中的值是否为单个数字的最佳方法是什么。我一直在做类似

var valAsString = '' + val;
if (valAsString.match(/\d/) {}

澄清的事情:我的意思是 0,1,2,3,4,5,6,7,8,9 之一

另外,我应该做些什么吗?我很惊讶人们为此想出了这么多不同的方法。

whats the best way to tell if a value in javascript is a single digit. Ive been doing something like

var valAsString = '' + val;
if (valAsString.match(/\d/) {}

clarification: I mean one of 0,1,2,3,4,5,6,7,8,9

Also, should what I have work? Im surprised how many different ways people are coming up with for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ゝ偶尔ゞ 2024-10-03 06:21:55

/\d/ 正则表达式将匹配字符串中任意位置的数字,例如 "foo1" 将匹配 "1"

对于正则表达式方法,需要这样的方法,以确保字符串包含单个数字:

if (/^\d$/.test(val))  {
  //..
}

请注意,我使用的是 test 方法,当您只想检查字符串是否匹配时建议使用该方法该模式,以及 test 方法内部将转换为字符串参数。

另一种简短的非正则表达式方法:

function isDigit(val) {
  return String(+val).charAt(0) == val;
}

The /\d/ regexp will match a digit anywhere on a string, for example in "foo1" will match "1".

For a regexp approach need something like this, to ensure that the string will contain a single digit:

if (/^\d$/.test(val))  {
  //..
}

Note that I'm using the test method, which is recommended when you only want to check if a string matches the pattern, also, the test method internally will convert to sting the argument.

Another short non-regexp approach:

function isDigit(val) {
  return String(+val).charAt(0) == val;
}
感受沵的脚步 2024-10-03 06:21:55

嗯,检查一下它的字符串长度是否等于 1?

if (typeof(val) === "number")
    {
    var valAsString = val.toString(10);
    if (valAsString.length === 1) {}
    }

但这不接受负数或带有小数部分的数字。

Ummm, check if it's string length is equal to one?

if (typeof(val) === "number")
    {
    var valAsString = val.toString(10);
    if (valAsString.length === 1) {}
    }

This won't accept negative numbers or numbers with decimal components though.

人间☆小暴躁 2024-10-03 06:21:55

如果您不想包含负数,这将有效,您的解决方案也将有效。

var valAsString = val.toString();
if (valAsString.length === 1) {}

您可以只检查字符串是否在 -10 到 10 之间(假设您想包含负数)。这将是最快的,但不适用于非整数,因此最好避免它。

如果您确实想包含负数,我可能会检查该数字是否为整数,然后我会采用如下方法:

var isSingleDigitIncludingNegatives = function( arg ) {
    return ((typeof(myNum)=='number') && (myNum.toString().indexOf('.')==-1)) && (( -10 < arg ) && ( arg < 10))

If you don't want to include negatives this will work, as will your solution.

var valAsString = val.toString();
if (valAsString.length === 1) {}

You could just check if the string is between -10 and 10 (assuming you want to include negatives). This will be fastest, but will not work for non-integers, so its probably best avoided.

If you do want to include negatives I'd probably check to see if the number is an integer then I'd go with something like this:

var isSingleDigitIncludingNegatives = function( arg ) {
    return ((typeof(myNum)=='number') && (myNum.toString().indexOf('.')==-1)) && (( -10 < arg ) && ( arg < 10))
苄①跕圉湢 2024-10-03 06:21:55

假设 val 已经是数字...

if ((val >= 0) && (val < 10) && (Math.floor(val) == val)) {
    // ...
}

Assuming that val is already numeric...

if ((val >= 0) && (val < 10) && (Math.floor(val) == val)) {
    // ...
}
南笙 2024-10-03 06:21:55

您可以对正则表达式进行以下修改:

valAsString.match(/^\d$/)

You can use the below modification of your regular expression:

valAsString.match(/^\d$/)

ペ泪落弦音 2024-10-03 06:21:55

我认为

(+val + val % 1 + 10) % 10 === val

应该这样做,假设您只想接受 number 类型的值。

更干净的解决方案

typeof val === 'number' && val >>> 0 === val % 10

可以轻松地适应包括其他类型的值。

I think

(+val + val % 1 + 10) % 10 === val

should do it, assuming you only want to accept values of type number.

A cleaner solution is

typeof val === 'number' && val >>> 0 === val % 10

which can be easily adapted to include values of other types.

马蹄踏│碎落叶 2024-10-03 06:21:55

像这样的事情怎么样:

var check = parseFloat(val);
var isSingleDigit = (!isNaN(check) && (check < 10 && check > -10) && check.toString().length ==1);

How about something like this:

var check = parseFloat(val);
var isSingleDigit = (!isNaN(check) && (check < 10 && check > -10) && check.toString().length ==1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文