How to write the following regular express?

N

nickwang

123ABC123 => 123ABC, 123
343BBB => 343BBB, null
12323 => null, 12322

(?<first>.*\D+)(?<second>\d*) doesn't work on the third line.
 
R

RobG

123ABC123 => 123ABC, 123
343BBB => 343BBB, null
12323 => null, 12322

(?<first>.*\D+)(?<second>\d*) doesn't work on the third line.

Try:

var data = {
x : '123ABC456',
y : '343BBB',
z : '12345'
};

var p, t, u,
re = /(^\d+[^\d]+|^.*?)(\d*$)/g;

for (p in data) {
u = [];
while (t = re.exec(data[p])) {
u[0] = t[1] || null;
u[1] = t[2] || null;
}
alert('String: ' + data[p] + '\n\n' + u.join(' : '));
}


You could also use:

re = /(^\d+[^\d]+|^)(\d*$)/g;


or if ^\d doesn't suit (it will match any non-digit):

re = /(^\d+[A-Z]+|^)(\d*$)/g;


or

re = /(^\d+[a-z]+|^)(\d*$)/ig;


may be safer - take your pick. Tested in Safari, Firefox and Opera.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top