[英]Regular Expression to find a value in a string
I've got a bunch of strings that look like this: 我有一堆看起来像这样的字符串:
Using javascript, how can I get an array of 10,4,77? 使用javascript,如何获取10、4、77的数组? These will be values of input fields all with the class form-text so I have the option of iterating of them. 这些都是输入字段的值,所有输入字段都带有class form-text,因此我可以选择对其进行迭代。
Thanks, Howie 谢谢,豪伊
Lets say you have the given strings in array. 假设您在数组中有给定的字符串。 Then using Array.map
you can retrieve the id's 然后使用Array.map
您可以检索ID的
var a = ["This is string 1 [id:10]", "This is string 2 [id:4]", "This is string 3 [id:77]"];
var result = a.map(function (item) {
return /.*?\[id:([\d]{1,})\]/g.exec(item)[1];
});
console.log(result);
\d+(?=\])
Try this.See demo . 试试看。
https://regex101.com/r/vN3sH3/18 https://regex101.com/r/vN3sH3/18
You could do something like this: 您可以执行以下操作:
var test = "[id:10]";
var result = test.split(":")[1].split("]")[0];
//result should have 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.