gpt4 book ai didi

javascript - 使用 javascript/nodejs 分割 url 并存储在数组、对象或字符串中

转载 作者:行者123 更新时间:2023-12-03 10:10:41 25 4
gpt4 key购买 nike

假设我有一个 URL,例如/test-resource/test1-100/test2-200/test3-300

我想分割 url 并将 100、200 和 300 存储在数组、对象或字符串中。

最佳答案

我们来了

var str = '/test-resource/test1-100/test2-200/test3-300';
var re = /(test\d+)\-(\d+)/g;

var arr = [];

while( res = re.exec(str) ) {
arr[res[1]] = res[2];
alert('match:' + res[0] + ' property:' + res[1] + ' value:' + res[2]);
}

console.log(arr);

关于javascript - 使用 javascript/nodejs 分割 url 并存储在数组、对象或字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123774/

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