How to manage a string in an array

I have an array containing some fields

like this

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

I want to create a new array or manipulate this array so that it contains only

sid = {25,26,27}

from

_SID_25
_SID_26
_SID_27

where sid will be my array containing sid extracted from the above pattern array _SID_

I need to do this in jquery or javascript

+3
source share
3 answers

use jquery map + regexp

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 should be an array of values.

(provided that all values ​​in the array match the pattern)

+7
source

I would use regex here

var sid = []
var matches = "ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_17".match(/_SID_(\d+)/);
if(matches) sid.push(parseInt(matches[1]));
+4
source

, , "", , , , , . "SID_", , , .

:

s = "ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25344_SortOrder_17"

:

array.push(parseInt(s.split("SID_")[1].split("_")[0]))

array.push(parseInt(s.split("_")[7])
0
source

Source: https://habr.com/ru/post/1755921/


All Articles