Another Reg Exp problem

L

Larry Bud

Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc

TIA!
 
K

kaeli

Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

Try
/^\d[,\d]*$/

--
--
~kaeli~
Murphy's Law #2030: If at first you don't succeed, destroy
all evidence that you tried.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
L

Lee

Larry Bud said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc

If there must be at least two digits, then it's easy:
/^\d+[,\d]*\d$/

That's a digit at the beginning, any number of [comma or digit],
followed by a digit at the end.

If a single digit is also valid, then it's only a little more complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.
 
L

Lee

kaeli said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

Try
/^\d[,\d]*$/

That could end with a comma
 
L

Lasse Reichstein Nielsen

Try
/^\d[,\d]*$/

This can end with a comma. You probably mean
/^\d+(,\d+)*$/

Hmm. In fact, the original poster didn't say that the string must be
non-empty, so a more correct solution would be:
/^(\d+(,\d+)*)?$/

/L
 
L

Larry Bud

Lee said:
Larry Bud said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc

If there must be at least two digits, then it's easy:
/^\d+[,\d]*\d$/

That's a digit at the beginning, any number of [comma or digit],
followed by a digit at the end.

If a single digit is also valid, then it's only a little more complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.

Damn, I DID forget to add that it cannot be empty. Will this work for it as well?

What if someone enters

1,,23

There shouldn't be two commas in a row.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas.

And non-empty.

So you want a digit, followed by any number, including zero, of comma-
followed-by-at-least-one-digit - and nothing else.

OK = /^(\d+)(,\d+)*$/.test(S)

Tested in
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>

OK = /^(\d+,)*(\d+)$/.test(S)

should also do. In each case, the parentheses around just \d+ are
superfluous.
 
T

Thomas 'PointedEars' Lahn

Larry said:
Lee said:
Larry Bud said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma
[...]

If there must be at least two digits, then it's easy: /^\d+[,\d]*\d$/
[...]
If a single digit is also valid, then it's only a little more
complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.

Damn, I DID forget to add that it cannot be empty. Will this work for it
as well?
No.

What if someone enters

1,,23

It would be accepted using the above RegExp. /\d+/ matches "1",
/[,\d]*/ matches ",,2" and the final /\d/ matches "3" then.
There shouldn't be two commas in a row.

I think you are looking for /^(\d(,\d)?)+$/. Could be written as
/^(\d,\d|\d)+$/ as well. It remains to be discussed what of them
is more efficient.


HTH

PointedEars
 

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,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top