Wednesday, 15 July 2015

javascript - Capturing String Segments between Special Characters using Regular Expressions -



javascript - Capturing String Segments between Special Characters using Regular Expressions -

i have next string of text:

textstring1:textstring2:textstring3:textstring4

i'm looking capture each text string , assign them variables.

i've somehow managed come following:

var errortext = 'aaaa:bbbb:cccc:dddd'; var substring, intro, host, priority, queue = ''; var re = /(.+?\:)/g; substring = errortext.match(re); intro = substring[0]; host = substring[1]; priority = substring[2]; //queue = substring[3]; console.log(intro + " " + host + " " + priority);

js bin link

however, i'm having problems with:

capturing lastly group, since there no : @ end the variables contain : i'd strip

you don't need regex - utilize errortext.split(':') split colon. homecoming array.

and if want add together them spaces, simple replace instead: errortext.replace(/:/g,' ').

javascript regex

No comments:

Post a Comment