不幸的是,我找不到这个具体问题的答案。我有一个字符串:
task[orders_attributes][4][line_items_attributes][2][id]
我知道如果只有一个号码,我可以使用.match(/\d+/)
但是如何从中仅提取2
?
该数字的范围为0到9999999999999,因此需要考虑该数字的字符串长度,并且字符串的第一个数字(orders_attributes
之后的那个)也将是动态的。文本和括号是静态的。
答案 0 :(得分:2)
您必须使用let str = "task[orders_attributes][4][line_items_attributes][2][id]";
let pattern = /\d+/g;
let matches = str.match(pattern);
console.log(matches[1]); //Use matches[1] to get the second number.
正则表达式模式。这会将所有数字返回到数组。
index <- x < 0.09
x[index] <- 1
x[! index] <- 0
x
# [1] 1 1 0 1 1 1 1 1 1
答案 1 :(得分:1)
您可以尝试以下内容
let str = "task[orders_attributes][45][line_items_attributes][23][id]";
let pat = /(\[\d+\])/g;
let matches = str.match(pat);
console.log(matches[1]); //Use matches[1] to get the second number.
&#13;