最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for list of comma-separated floats - Stack Overflow

programmeradmin7浏览0评论

I am trying to validate a form field in javascript (using bootstrap/jquery) but I need a regular expression which matches a string which is a ma separated list of numbers, there may or may not be spaces.

example: 1,2,3,3.14,6.0, -3.14, -6, 7.13,100

I can get the regex if they are all integers and no spaces in between, but having decimals really plicates things.

I am trying to validate a form field in javascript (using bootstrap/jquery) but I need a regular expression which matches a string which is a ma separated list of numbers, there may or may not be spaces.

example: 1,2,3,3.14,6.0, -3.14, -6, 7.13,100

I can get the regex if they are all integers and no spaces in between, but having decimals really plicates things.

Share Improve this question asked Mar 29, 2016 at 5:49 user4797334user4797334
Add a ment  | 

4 Answers 4

Reset to default 6

You could try this:

^(\s*-?\d+(\.\d+)?)(\s*,\s*-?\d+(\.\d+)?)*$

document.write(
    /^(\s*-?\d+(\.\d+)?)(\s*,\s*-?\d+(\.\d+)?)*$/.test(
        '1,2,3,3.14,6.0, -3.14, -6, 7.13,100'
    )
);

If you break up the above regex, you'll notice that it is capturing the first number in the string with:

(\s*-?\d+(\.\d+)?)

To capture the first number, it tries matching as many consecutive spaces \s* as necessary, followed by an optional hyphen (or negative sign) -?, followed by at least one numeric digit \d+, followed by an optional decimal point that has at least one consecutive numeric digit following the decimal point (\.\d+)?.

The next group captures all the numbers following the first one.

(\s*,\s*-?(\d+(\.\d+)?)*

is the same as the previous group except for an additional \s*, in front allowing for as many spaces as necessary \s* before matching a ma ,. This group is repeated as many times as necessary *.

The regex starts with ^ and ends with $ to make sure it starts matching from the beginning of the string ^ until the end of the string $.

If regex is not mandatory, then try this simple logic

function isValidInput(str)
{
   return str.split(",").filter(function(val){ return isNaN(val) }).length == 0;
}
isValidInput("1,2,3,3.14,6.0, -3.14, -6, 7.13,100");

An alternate to @gurvinder372's answer, you can use Array.every instead of Array.filter. Benefit, it will not loop till the end and will break on first invalid entry. But, if you wish to get all incorrect values, then @gurvinder372's answer is preferred.

function isValidInput(str) {
  return str.split(",").every(function(val) {
    return !isNaN(val);
  })
}
var test = isValidInput("1,2,3,3.14,6.0, -3.14, -6, 7.13,100");

alert(test);

You can try this

^(\s*(-|\+)?\d+(?:\.\d+)?\s*,\s*)+(-|\+)?\d+(?:\.\d+)?\s*$

Regex breakdown

^ ---> Starting of string
  (\s* ---> Match any number of white spaces
     (-|\+)? ---> Match - or + sign (optional)
     \d+ ---> Match digits before decimal
     (?:\.\d+)? ---> Non capturing group to match digits after decimal(optional)
     \s*,\s* ---> Match any number of spaces before and after ma
  )+ ---> Do this till last ma (or second last number)

  (-|\+)?\d+(?:\.\d+)? ---> Same as above but for checking only the last number as it should not have ma
  \s* ---> Check spaces after last number
$ ---> End of string

Demo

发布评论

评论列表(0)

  1. 暂无评论