[英]Regular Expression to find a value in a string
我有一堆看起來像這樣的字符串:
使用javascript,如何獲取10、4、77的數組? 這些都是輸入字段的值,所有輸入字段都帶有class form-text,因此我可以選擇對其進行迭代。
謝謝,豪伊
假設您在數組中有給定的字符串。 然后使用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);
您可以執行以下操作:
var test = "[id:10]";
var result = test.split(":")[1].split("]")[0];
//result should have 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.