capture with regex in javascript -


i have string "listui_col_order[01234567][5]". i'd capture 2 numeric sequences string. last part between square brackets may contain 2 digits, while first numeric sequence contains 8 digits (and numbers dynamically changing of course.) im doing in javascript , code first part simple: 8digit sequence string:

var str = $(this).attr('id'); var unique = str.match(/([0-9]){8}/g); 

getting second part bit complicated me. cannot use:

var column = str.match(/[0-9]{1,2}/g) 

because match '01', '23', '45', '67', '5' in our example, it's clear. although i'm able information need column[4], because first part contains 8 digits, i'd nicer way retrieve last number. define contex , can tell regex im looking 1 or 2 digit number has square brackets directly before , after it:

var column = str.match(/\[[0-9]{1,2}\]/g) // return [5]. want 

so numeric data use parenthesis capture numbers like:

 var column = str.match(/\[([0-9]){1,2}\]/g)  // result in:  // column[0] = '[5]'  // column[1] = [5] 

so question how match '[5]' capture '5'? have [0-9] between parenthesis, still capture square brackets well

you can both numbers in 1 go :

var m = str.match(/\[(\d{8})\]\[(\d{1,2})\]$/) 

for example, makes ["[01234567][5]", "01234567", "5"]

to both matches numbers, can do

if (m) return m.slice(1).map(number) 

which builds [1234567, 5]


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -