admin管理员组

文章数量:1291122

I am trying to figure out how to use Regex in looking for a comma. I have a form where a user will submit information separated by a comma and I need to verify there is not a comma at the end, or multiple comma together, etc.

I tried this, ^(\w+)(,\s*\w+)*$, however it did not work because it failed when I added more than 1 word within a , bracket.

Invalid: hello,,,,,,

Invalid: hello, world, , how, are, you

Invalid: hello, world,

Valid: Hello, World

Valid: Hello, World, how are you, doing, today

Valid: .5 cups, 1.5 cups

I am trying to figure out how to use Regex in looking for a comma. I have a form where a user will submit information separated by a comma and I need to verify there is not a comma at the end, or multiple comma together, etc.

I tried this, ^(\w+)(,\s*\w+)*$, however it did not work because it failed when I added more than 1 word within a , bracket.

Invalid: hello,,,,,,

Invalid: hello, world, , how, are, you

Invalid: hello, world,

Valid: Hello, World

Valid: Hello, World, how are you, doing, today

Valid: .5 cups, 1.5 cups
Share Improve this question edited 2 days ago Peter Thoeny 7,6161 gold badge13 silver badges22 bronze badges asked Jan 2 at 19:39 letsCodeletsCode 2,9461 gold badge17 silver badges41 bronze badges 4
  • 4 Do you have to do this with a regexp? Why not use string.split(',') then check that there are no empty strings in the result? – Barmar Commented Jan 2 at 19:42
  • 1 i suppose I can do that.... @Barmar – letsCode Commented Jan 2 at 19:43
  • 3 Or check for the opposite, invalid if it matches , *(?:,|$) – bobble bubble Commented Jan 2 at 21:22
  • 2 Also note that \w does not match . – The fourth bird Commented Jan 3 at 10:19
Add a comment  | 

2 Answers 2

Reset to default 4

You may use this regex to validate your inputs:

^[^,\n]+(?: *, *[^,\s][^,\n]*)*$

RegEx Demo

RegEx Details:

  • ^: Start
  • [^,\n]+: Match a text that starts 1 or more non-comma, non-newline characters
  • (?:: Start non-capture group
    • *, *: Match a comma optionally surrounded by 0 or more spaces
    • [^,\s][^,\n]*: Match a text that starts with a non-comma, non-whitespace character followed by 0 or more non-comma, non-newline characters
  • )*: End non-capture group. Repeat this group 0 or more times
  • $: End

Code Demo:

const rx = /^[^,\n]+(?: *, *[^,\s][^,\n]*)*$/;
const input = ['hello,,,,,,',
'hello, world, , how, are, you',
'hello, world,',
' hello , world , how , are, you',
'Hello, World',
'Hello, World, how are you, doing, today',
'.5 cups, 1.5 cups']

input.forEach(el => 
  console.log(rx.test(el) ? "Valid:" : "Invalid:", el)
)

You can try something like:

^(?:[^\r\n,]*[^\r\n\s,]\s*,)*[^\r\n,]+$

Details:

  • (?:[^\r\n,]*[^\r\n\s,]\s*,)*: This group is for at least one non-whitespace char, followed by a comma, repeated zero or more times.
  • [^\r\n,]+: This is for one non-whitespace char without any comma

本文标签: javascriptRegexvalid string has comma separating wordsStack Overflow