0

Hello,

I am trying to parse a string using regular expressions. This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. Here are some examples of the data:

00923:5342:123 --> I want to extract 00923, 5342 and 123 into an array

08765::764375 --> parse this as 08765 and 764375 into an array

3246//672354//23458 --> parse as 3246, 672354 and 23458

23564-73654 --> etc

I'm using the following code:

var ids = str.match(/\w*/g);

But it just returns the first token from each line and ignores the rest. So with the above data I'm only getting back 00923, 08765, 3246 and 23564 into the first element of the "ids" array.

I've also tried an "inverse" approach using the split method like so:

var ids = str.split(/\W*/g);

and I just get the same result - the first token only.

My test code looks like this ('str' contains a string read in from a file):

var ids = str.match(/\w*/g);
//var ids = str.split(/\W*/g);

task.logmsg("the length of the array is " + ids.length);

for (i = 0; i < ids.length; i++) {
    task.logmsg("ids=[" + i + "]=" + ids[i]);
}

I can confirm that ids.length is returning 1 (??)

The 'task' object comes from a separate library and the 'logmsg' method just writes to the console.

My reptilian brain is struggling with reg exps. This shouldn't be difficult to do, but I just can't get it to work.

If anyone out there can lend some assistance, it would be greatly appreciated.

Thanks,
JD

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Troy III
0

use:

    str.match(/d+/g);

in

    var ids = str.match(/d+/g);

ids wil be a ready array, so you can...

    for(var i=0,j=ids.length; i<j;i++) task.logmsg("ids = ["+i+"] = "+ ids[i]);

Edited by Troy III: code format went south

0

Hi Troy,

Thanks for your reply. Unfortunately it does not work. The javascript interpreter throws an error on your code . Doesn't the character class 'd' have to be escaped with "\"? Anyway, I tried your code using "/\d+/g" and I get the same result as I reported in my original post. I am using 'w' because there is potential for alpha characters to appear in the input (although my test data only shows digits).

Thanks,
JD

0

Yellow,

Try str.split(/\W+/g). This will split at any group of one or more non-word characters.

Demo

Edited by Airshow: Added text

0

Your question:

This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. I want to extract 00923, 5342 and 123 into an array from a string like var str = "00923:5342:123"

If what you wrote is exactly what you meant[?] -than my answer to you is absolutely correct:

console.log( str.match(/\d+/g) );
>> LOG: [00923, 5342, 123]

The escape part was already corrected by you - so I don't see any possible problems with it.
So what is your real problem/task here?

0

(the complete answer)

Your question:

This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. I want to extract 00923, 5342 and 123 into an array from a string like var str = "00923:5342:123"

If what you wrote is exactly what you meant[?] -than my answer to you is absolutely correct:

console.log( str.match(/\d+/g) );
>> LOG: ["00923", "5342", "123"]

further more...
The:

ids =  str.match(/\d+/g) ;

//with:
for(var i=0,j=ids.length; i<j;i++)console.log("ids = ["+i+"] = "+ids[i])
// will:
>> ids = [0] = 00923
   ids = [1] = 5342
   ids = [2] = 123

The escape part was already corrected by you - so I don't see any possible problems with it.
So what is your real problem/task here?

Attention!
The extracted numbers on the returned array of matches are of string type, you will either relly on a duck-type conversion syntax or transform them explicitly onto a proper number objects.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.