簡體   English   中英

正則表達式以查找字符串中的值

[英]Regular Expression to find a value in a string

我有一堆看起來像這樣的字符串:

  • 這是字符串1 [id:10]
  • 這是字符串2 [id:4]
  • 這是字符串3 [id:77]

使用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);
\d+(?=\])

試試看。

https://regex101.com/r/vN3sH3/18

您可以執行以下操作:

var test = "[id:10]";
var result = test.split(":")[1].split("]")[0];
//result should have 10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM