Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to write the following regular express?

Reply
Thread Tools

How to write the following regular express?

 
 
nickwang@gmail.com
Guest
Posts: n/a
 
      07-17-2007
123ABC123 => 123ABC, 123
343BBB => 343BBB, null
12323 => null, 12322

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

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-17-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.



--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
What's wrong with the following regular expression? kun niu Perl Misc 14 04-21-2009 08:56 PM
What does m@ stand for in the following regular expression. grocery_stocker Perl Misc 13 10-06-2008 06:30 AM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments