gsamaras gsamaras - 1 year ago 155
HTML Question

Split string that used to be a list

I have this code:

var lines = this.result.split('\n');
for (var line = 0; line < lines.length; line++) {
var sublines = lines[line].split(' ');
for (var subline = 0; subline < sublines.length; subline++) {

which I would hope that it would extract the tokens from the string and then parse the integer from every token, but it seems like
split(" ")
is not what will work here!

Here is what I see:

(0, (u'5643145391', u'11367866245'))

getting consoled twice, which should mean that no split is done. In my real data the list with the big numbers is 150 in length, but that shouldn't matter.

How to to split that (partially unicode) string?

jsFiddle to reproduce the issue. However it seems that the fiddle prints the strings only once, without annotating how many times this message printed.

Desired output would be, a line per iteration:


but anything close to this would be appreciated.

Answer Source

This seems like something that a RegExp could be useful for,

(                              // begin capture group
  -?                           // match 0 or 1 minus sign
  \d{1,}                       // match 1 to unlimited digits
)                              // end capture group

var line = "(0, (u'5643145391', u'11367866245'))", 
    regex = /(-?\d{1,})/g;

console.log( line.match(regex) );

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download