Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检索正则表达式匹配组值_Javascript_Arrays_Regex - Fatal编程技术网

Javascript 检索正则表达式匹配组值

Javascript 检索正则表达式匹配组值,javascript,arrays,regex,Javascript,Arrays,Regex,这是剧本;: var output = [] output = this.getField("SelectedSoftware").value.match(/\$(\d{1,4}\.\d\d)/g); 字段中的示例文本: 自动化、试听($500.00)、Citrix接收器、AutoCAD($54.93)、Clarity Studio($748.23)、Audacity($300.00)、试听($500.00)、Business Objects仪表板、试听($500.00) 我遇到的问题是只获取

这是剧本;:

var output = []
output = this.getField("SelectedSoftware").value.match(/\$(\d{1,4}\.\d\d)/g);
字段中的示例文本:

自动化、试听($500.00)、Citrix接收器、AutoCAD($54.93)、Clarity Studio($748.23)、Audacity($300.00)、试听($500.00)、Business Objects仪表板、试听($500.00)

我遇到的问题是只获取成本数字,这样就可以将它们相加以显示总数<代码>(?)? 我得到的只是成本数字,这样它们就可以加在一起显示总数。 始终根据需要返回数组

看到这个了吗

输出:

["$500.00", "$54.93", "$748.23", "$300.00", "$500.00", "$500.00"]
现在,您可以使用这个正则表达式
/(\d{1,4}\.\d\d)/g
简单地添加所有数字


获取这些值的总和:

var total = 0;
for(var _=0; _<output.length; _++)
    total += parseFloat(output[_]);
var总计=0;
对于(var\u0;\up>,您可以使用
(?:)
创建非捕获组,但我认为在这种情况下不需要它

exec:在字符串中执行匹配搜索的RegExp方法。 它返回一个信息数组。
test:一种用于测试的RegExp方法 用于字符串中的匹配。它返回true或false。
匹配:字符串 方法,该方法在字符串中搜索匹配项。它返回 不匹配的信息数组或null。
搜索:字符串方法 测试字符串中的匹配项。它返回匹配项的索引, 如果搜索失败,则为-1。
替换:执行 在字符串中搜索匹配项,并替换匹配的子字符串 带有替换子字符串。
拆分:使用 正则表达式或固定字符串,用于将字符串拆分为数组 子字符串的类型

您需要的是RegExp.exec,它将返回一个信息表单数组,您可以从中获取捕获的组

它返回一个数组,其中包含
[0]
中的整个匹配项,然后将每个捕获的组放入以下数组插槽中。因此,您需要的是:

var price = /\$(\d{1,4}\.\d\d)/.exec("$59.99")[1];
RegExp.exec(string)
应该非常有用。事实上,这是从字符串中获取捕获组的唯一方法。不过实现起来有点困难,因为
.exec()
只返回一个结果

示例代码:

var regex = /\$(\d{1,4}\.\d\d)/g,
    text = this.getField("SelectedSoftware").value,
    prices = [],
    total = 0,
    num;

// Assignments always return the value that is assigned.
// .exec will return null if no matches are found.
while((val = regex.exec(text)) !== null){
  // Capture groups are referenced with 'val[x]', where x is the capture group number.
  num = parseFloat(val[1]);
  total += num;
  prices.push(num);
}

这是因为
RegExp
对象有一个名为
.lastIndex
的属性,它是下一个
.exec()
搜索的基础


您可以。

总是返回一个根本不回答问题的数组。现在似乎已经完成;)使用
parseInt()
可能不是一个好主意,因为有些数字不是整数。要保留十进制数字,最好使用
parseFloat()
。所以我调整了一些正则表达式,没有使用
parseInt
parseFloat
。请原谅我打扰@GrandonBrosephThanks@GrandonBroseph。除了使用parseFloat,这正是我需要的。
var regex = /\$(\d{1,4}\.\d\d)/g,
    text = this.getField("SelectedSoftware").value,
    prices = [],
    total = 0,
    num;

// Assignments always return the value that is assigned.
// .exec will return null if no matches are found.
while((val = regex.exec(text)) !== null){
  // Capture groups are referenced with 'val[x]', where x is the capture group number.
  num = parseFloat(val[1]);
  total += num;
  prices.push(num);
}