0

Here is the pattern on which i'm Working :

/^\d+((.\d+))?(,\d+((.\d+))?)*$/

Its supports

1.2,30.5,13.54

25.65

But i want the pattern which supports following:

1.3,.3,4.5

.3,.4,.6

.2

1.3,5.6,.5

4 Answers 4

2

Based on your given examples, you require a decimal part, so you could use

/(\d*\.\d+),?/

This will match every given example. In case you also want to match numbers without a decimal dot, just add a second match:

/(\d*\.\d+|\d+),?/

Oh, and for JavaScript, to add the "g" modifier (for global search), you need to add it as a second parameter to the RegExp constructor:

re = new RegExp("(\d*\.\d+|\d+),?", "g");

Example: http://regex101.com/r/vL5aT0

Okay, I don't know on what purpose you are matching your strings. If you just want to validate them and they should exactly look like in your examples, use this:

/((\d*\.\d+|\d+),?)*(\d*\.\d+|\d+)$/

Thanks to Elergy for pointing me to this. By the way, Elergy's regex also matches useless lines of only periods and commas, like

.3,.4,.5,,,,8,.,7.,.,.,.,4.,.,.,.,.9,.,,,4.,,1,,
Sign up to request clarification or add additional context in comments.

2 Comments

Note you can also add the g flag by doing /regex/g.
Didn't knew that. Always thought, I need this blown up constructor for RegEx in JavaScript. Thanks :)
1
/(\d+)?\.\d+(,?)/

(\d+)? Match all digits but optional.
\d+(,?) Match all digits with a , as optional.

Comments

0

If my understanding of your problem is right, this expression can help you:

/^\d*[\.,]\d*(?:,\d*[\.,]\d*)*$/

1 Comment

If we entered single digit expression will fail. like 3. For all other input it works perfect.
0

What do you want to do with them afterwards? If you goal is to identify a comma separated list of valid floating point numbers, you can use the Number constructor, which returns NaN if the number is invalid (which evaluates to false):

"1.3,5.6,a.s".split(",").every(Number)
false
"1.3,5.6,.5".split(",").every(Number)
true

Personally I like to avoid regex where possible and I think that this is pretty self-documenting. split the string on , and check that every value is a valid number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.