views:

103

answers:

3

I have an array that contain 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 to contain only

sid = {25,26,27}

from

_SID_25
_SID_26
_SID_27

where sid will be my array containing sid's extracted from above array with pattern _SID_

I have to do this in jquery or javascript

+4  A: 

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]));
Anpher
@Anpher: Could you please provide more details on how would it get me the digit in array ?
Shantanu Gupta
The "(\d+)" part in regex is considered to accept only digits
nemke
I would add an `assert( matches.count == 1 )`.
xtofl
@anpher. you're ignoring the requirement to go through the array and output an array.
Ben
+7  A: 

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 the values..

(assuming all the values in the array do match the pattern)

Ben
@Ben: why you used `[1]` in `this.match(/SID_(.*?)_/)[1]`. What task does it accomplish.
Shantanu Gupta
@Shantanu. match returns an array, with the first item (0) the whole matched expression and the second (1) the first capture group, ie. the (.*?) expression.Try adding console.log(this.match(/SID_(.*?)_/)) before the return and run it in firefox+firebug to see the results of the match function
Ben
Especially like the usage of `map` to obtain a transformed array.
xtofl
I deleted my answer, I wrote it on my iPhone so I couldn't be as descriptive as I wanted to be, but the reason I used `this.split("_")[6]` instead is that it is much more readable and requires no knowledge of regular expressions.
Andy E
@Andy. True. But I assume all that crazy string construction comes from using asp.net controls (right?). If the control is moved elsewhere or nested in or out of another, the number of _ will change and the index won't work anymore..
Ben
@Ben: you should add `.get()` at the end of `.map()` to return a true array. Updated that.
jAndy
@Ben: hmm I didn't think about that - good point. +1 to your answer :-)
Andy E
Don't use `$.fn.map` for regular arrays... use `$.map`, and then you won't have to fuss with `.get()`...
J-P
Ben
A: 

This solution is totally reliant on the overall string form not changing too much, ie the number of "underscores" not changing which seems fragile, props given to commenter below but he had the index wrong. My original solution first split on "SID_" since that seemed more like a key that would always be present in the string going forward.

Given:

s = "ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25344_SortOrder_17"

old solution:

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

new solution

array.push(parseInt(s.split("_")[7])
nicholasklick
Nice, except that it works for 2-digit numbers only.
xtofl
why not just `s.split("_")[6]`?
Andy E