31

Hey, I can't figure out how to write a regular expression for my website, I would like to let the user input a list of items (tags) separated by comma or by comma and a space, for example "apple, pie,applepie". Would it be possible to have such regexp? Thanks!

EDIT: I would like a regexp for javascript in order to check the input before the user submits a form.

Masiar
  • 20,450
  • 31
  • 97
  • 140

10 Answers10

48

What you're looking for is deceptively easy:

[^,]+ 

This will give you every comma-separated token, and will exclude empty tokens (if the user enters "a,,b" you will only get 'a' and 'b'), BUT it will break if they enter "a, ,b".

If you want to strip the spaces from either side properly (and exclude whitespace only elements), then it gets a tiny bit more complicated:

[^,\s][^\,]*[^,\s]*

However, as has been mentioned in some of the comments, why do you need a regex where a simple split and trim will do the trick?

Bennor McCarthy
  • 11,415
  • 1
  • 49
  • 51
  • I just need to check that in javascript, then I will do the explode / trim trick in PHP to get out the array of items :) thanks anyway! – Masiar Feb 15 '11 at 09:18
  • By the way, in javascript would this do the trick: new RegExp(/[^,]+/); ? – Masiar Feb 15 '11 at 09:22
  • It should do, but you should be able to simplify it to `"apple, pie,applepie".match(/[^,]+/g);` Have a look here: http://jsfiddle.net/SD6Bt/ – Bennor McCarthy Feb 15 '11 at 09:29
  • The 'g' is necessary to tell it to match as many times as it can. – Bennor McCarthy Feb 15 '11 at 09:30
  • 1
    The more complicated solution is not trimming the ending space: https://jsfiddle.net/2k9or1b7/2/ – techguy2000 Dec 21 '17 at 00:32
  • 1
    I do not think this is correct. Try "cat, dog , ant( elephant , lion(tiger)), bird" input and one element will be "elephant ". So the spaces after the match are not skipped. – user3742309 Jan 20 '20 at 12:43
5

Assuming the words in your list may be letters from a to z and you allow, but do not require, a space after the comma separators, your reg exp would be [a-z]+(,\s*[a-z]+)*

This is match "ab" or "ab, de", but not "ab ,dc"

David
  • 59
  • 1
  • 2
3

Here's a simpler solution:

    console.log("test, , test".match(/[^,(?! )]+/g));

It doesn't break on empty properties and strips spaces before and after properties.

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
3

This thread is almost 7 years old and was last active 5 months ago, but I wanted to achieve the same results as OP and after reading this thread, came across a nifty solution that seems to work well

.match(/[^,\s?]+/g)

Here's an image with some example code of how I'm using it and how it's working

enter image description here

Regarding the regular expression... I suppose a more accurate statement would be to say "target anything that IS NOT a comma followed by any (optional) amount of white space" ?

jsdev17
  • 1,050
  • 2
  • 15
  • 25
3

I often work with coma separated pattern, and for me, this works :

((^|[,])pattern)+

where "pattern" is the single element regexp

peeebeee
  • 2,541
  • 6
  • 21
  • 26
Seb
  • 41
  • 1
  • 2
    I like the idea for complex pattern you don't want to repeat: `^(?!,)((^|, ?)pattern)+$` allows to check for a coma or coma space separated list of `pattern`. This should be equivalent to `^pattern(, ?pattern)*` – tbussmann Sep 11 '20 at 13:10
2

This might work:

([^,]*)(, ?([^,]*))*
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0
([^,]*)

Look For Commas within a given string, followed by separating these. in regards to the whitespace? cant you just use commas? remove whitespace?

0

I needed an strict validation for a comma separated input alphabetic characters, no spaces. I end up using this one is case anyone needed:

/^[a-z]+(,[a-z]+)*$/

Or, to support lower- and uppercase words:

/^[A-Za-z]+(?:,[A-Za-z]+)*$/

In case one need to allow whitespace between words:

/^[A-Za-z]+(?:\s*,\s*[A-Za-z]+)*$/
/^[A-Za-z]+(?:,\s*[A-Za-z]+)*$/
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Filgaia
  • 1,046
  • 10
  • 11
-1

You can try this, it worked for me:

/.+?[\|$]/g

or

/[^\|?]+/g 

but replace '|' for the one you need. Also, don't forget about shielding.

user815129
  • 2,236
  • 2
  • 20
  • 40
-10

something like this should work: ((apple|pie|applepie),\s?)*

kusma
  • 6,516
  • 2
  • 22
  • 26