gpt4 book ai didi

javascript - 用于在字符串中查找值的正则表达式

转载 作者:行者123 更新时间:2023-11-30 10:09:04 24 4
gpt4 key购买 nike

我有一堆看起来像这样的字符串:

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

如何使用 javascript 获取 10,4,77 的数组?这些将是所有具有类 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);

关于javascript - 用于在字符串中查找值的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com