gpt4 book ai didi

javascript - 如何操作数组中的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:15 26 4
gpt4 key购买 nike

我有一个包含一些字段的数组

像这样

ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_17
ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_18
ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_19

我想创建一个新数组或操纵这个数组只包含

sid = {25,26,27}

来自

_SID_25
_SID_26
_SID_27

其中 sid 将是我的数组,其中包含从上述数组中提取的 sid使用模式 _SID_

我必须在 jquery 或 javascript 中执行此操作

最佳答案

使用 jquery 映射 + 正则表达式

var arr= ['tl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_17',
'ctl00_ctl00_cphBody_bodycph_content_rdo_SID_26_SortOrder_18',
'ctl00_ctl00_cphBody_bodycph_content_rdo_SID_27_SortOrder_19']

var out = $(arr).map(function(){
return this.match(/SID_(.*?)_/)[1];
});

out 应该是值的数组..

(假设数组中的所有值都匹配模式)

关于javascript - 如何操作数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3306556/

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