如何在JavaScript中从字符串中提取数字? [英] How to pull a number out of a string in JavaScript?

查看:88
本文介绍了如何在JavaScript中从字符串中提取数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 JavaScript 中的字符串中间拉出一个数字。在 Ruby (我的主要语言)中,我会这样做:

I want to pull a number out the middle of a string in JavaScript. In Ruby (my main language) I would do this:

Ruby:

name = "users[107][teacher_type]"

num = name.scan(/\d+/).first

但是JavaScript我必须这样做,这看起来有点笨拙。

But in JavaScript I have to do this, which seems a bit clunky.

JavaScript:

JavaScript:

var name = "users[107][teacher_type]"

var regexp = new RegExp(/\d+/)

var num = regexp.exec(name)[0]

有没有办法在不构建RegExp对象的情况下拉出匹配的部分?即Ruby的String#scan的单行代码?

Is there way to pull out the matching parts without building a RegExp object? I.e. a one-liner equivalent of Ruby's String#scan?

另外,作为旁注,因为这个字符串总是具有相同的格式,所以我可以使用它。更换。这不是一个聪明的解决方案,但我再次遇到JavaScript问题。

Also, as a side note, since this string will always have the same format, I could potentially do it using .replace. This isn't as clever a solution but again I have problems in JavaScript.

在Ruby中:

num = name.gsub(/users\[|\]\[teacher_type\]/,"")

但是我在JavaScript中尝试这个,它不喜欢正则表达式中间的or(|):

But when I try this in JavaScript it doesn't like the or (|) in the middle of the regex:

在JavaScript中:

In JavaScript:

//works

num = name.replace(/users\[/, "").replace(/\]\[teacher_type\]/,"")

//doesn't work

num = name.gsub(/users\[|\]\[teacher_type\]/,"")

任何人都可以让我直截了当吗?

Can anyone set me straight?

推荐答案

在创建动态正则表达式时,您只需要使用新的RegExp()部分。您可以在其他时间使用文字。 / \d + /相当于新的RegExp(\\d +)。请注意,在使用后者时必须转义特殊字符。

You only need to use the new RegExp() part when creating dynamic regular expressions. You can use literals at other times. /\d+/ is the equivalent of new RegExp("\\d+"). Note that you have to escape special characters when using the latter.

另外值得注意的是字符串#matc 返回null或数组。基于提供的解决方案( parseInt(name.match(/ \d + /),10)),这一点并不明显。恰好在传递给 parseInt 时将其转换为字符串。 ParseInt 将字符串值转换为整数(如果可能)。

Also noteworthy is that String#match returns null or an array. This is not apparent based on the supplied solutions (parseInt(name.match(/\d+/), 10)). It just so happens that it is converted to a string when passed to parseInt. ParseInt converts string values to integers (when possible).

name.match(/\d+/)[0]
/\d+/.exec(name)[0]

在这种情况下,这两个功能相同。

Those two are functionally identical in this case.

您指的其他匹配(否定匹配)需要特殊标记。要复制gsub的功能,您需要告诉正则表达式使用 g 标志多次应用。

The other match you were referring to (the negative matching) requires a special flag. To duplicate the functionality of gsub you need to tell the regex to be applied more than once with the g flag.

'users[107][teacher_type]'.replace(/users\[|\]\[teacher_type\]/g,'')

或者如果由于某种原因你必须使用新的RegExp 完成与上面相同的事情:

Or if you had to use new RegExp for some reason you'd accomplish the same as above like so:

'users [107] [teacher_type]'。replace(new RegExp('users\ [| \] \ [ teacher_type \]','g'),'')

'users[107][teacher_type]'.replace(new RegExp('users\[|\]\[teacher_type\]', 'g'),'')

再次注意我是如何逃避所有反斜杠的。 Mozilla的开发人员中心是一个很好的参考,以熟悉javascript中的正则表达式。

Notice again how I had to escape all the backslashes. Mozilla's Developer Center is a good reference to familiarize yourself with regex in javascript.

这篇关于如何在JavaScript中从字符串中提取数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆