regex - Regular Expression in JavaScript for String Comma Separated -


i have string follows

new york, new york, united states

i need regular expression ether @ least 1 comma or @ least 2 comma or 3 words 2 commas in between .

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

you can use this.replace * {1,2} if want 3 words.see demo.

^[^,\n]+,(?:[^,\n]+,?){0,2}$ 

https://regex101.com/r/bw3ar1/11

var re = /^[^,\n]+(?:,[^,\n]+)*$/gm; var str = 'new york, new york, united states\nnew york, new york\nnew york\nnew york, new york, united states,'; var m;  while ((m = re.exec(str)) != null) { if (m.index === re.lastindex) { re.lastindex++; } // view result using m-variable. // eg m[0] etc. } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -