1

I am not good with JavaScript Regex, so I need help checking my string for validation.

I am getting these image names separated by comma: Chrysanthemum.jpg,Desert.png,Hydrangeas.gif,Jellyfish.jpg,

Now I want to check with this regex:

What is valid:

Chrysanthemum.jpg,Desert.png,Hydrangeas.gif,Jellyfish.jpg,Koala.jpg,Lighthouse.png,

What is not valid:

1. Chrysanthemum.jpg,Desert.png,Hydrangeas.gif,
2. Chrysanthemum.jpg,Desert.png,
3. Chrysanthemum.jpg,

Validation should only succeed when the amount of comma separated values are more than 3.

Manan
  • 1,197
  • 4
  • 15
  • 25
  • Are you going to separate and use each image name anyway? – mowgli Jul 25 '14 at 08:36
  • Do you need to check values (image names) in your comma-separated list or you just need to know their quantity? Why not use split (http://www.w3schools.com/jsref/jsref_split.asp)? As a bonus - empty elements check - http://stackoverflow.com/a/2843625/2571926. – Ilya Luzyanin Jul 25 '14 at 08:36

3 Answers3

1

Option 1: Count the Commas Followed by Text

Here is a simple approach:

if (/(?:,\s*\S+){3}/.test(yourString)) {
    // It matches!
} else {
    // Nah, no match...
}

This checks that there are at least three commas followed by optional spaces, then non-space characters.

In the Regex Demo, you can see how this works (there is a match).


Option 2: Check that You Have At Least Four Images

This option checks that you have at least four images.

if (/^\w+\.(?:jpg|png|gif)(?:, \w+\.(?:jpg|png|gif)){3}/.test(yourString)) {
    // It matches!
} else {
    // Nah, no match...
}

See the matches in the Regex Demo.

zx81
  • 41,100
  • 9
  • 89
  • 105
1

The below regex would validates for filename extensions, spaces after the comma except the last one and the comma seperated values are more than 3,

^[A-Z][a-z]+\.(?:jpg|png|gif),(?: [A-Z][a-z]+\.(?:jpg|png|gif),){3,}$

DEMO

If the values are not seperated by comma and space then your regex would be,

^[A-Z][a-z]+\.(?:jpg|png|gif),(?:[A-Z][a-z]+\.(?:jpg|png|gif),){3,}$

DEMO

OR

A much simpler one,

^(?:[A-Z][a-z]+\.(?:jpg|png|gif),){4,}$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Hi regex is working but in my string there is no space between images name like `Salvis.jpg,Sumeet.jpg,Jacob.png,Srlawr.png,` so it's not working without space. – Manan Jul 25 '14 at 08:40
  • This is valid: `Salvis.jpg,Sumeet.jpg,Jacob.png,Srlawr.png,` and this is not valid with space: `Salvis.jpg, Sumeet.jpg, Jacob.png, Srlawr.png,` – Manan Jul 25 '14 at 08:41
  • Okay now can you explain me if in future I have to check comma separated value more then 5 what to change? – Manan Jul 25 '14 at 08:51
  • change last `{4,}` to `{5,}` . Just play with my regex on regex101 site. `{5,}` means from the starting it must have 5 filenames. – Avinash Raj Jul 25 '14 at 08:52
0

Are you sure you need regex for that? Try this:

var str = "Chrysanthemum.jpg, Desert.png, Hydrangeas.gif, Jellyfish.jpg,";
var length = str.split(',').filter(function(item) { return item != ""; }).length;
if (length > 3) {
  ...
}
Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49