Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
how to check string "should not contain leading, trailing, and mutliple commas in middle"

I am able to construct regex for leading and trailing.

(^[,\s]+)|([,\s]+$)

But I don't know to how to negate it, it should check for non-leading and non-trailing commas.

And it should not contain double or more commas (near to each other) in the middle.

// example - should return following values
1. "orange" - true
2. "apple, orange" - true
3. "apple,     orange" - true
4. "apple,orange" - true
5. "apple, orange," - false
6. ",orange, apple" - false
8. "orange,, apple" - false
9. "apple,,orange" - false

Any here does know how to achieve. thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would just use this general pattern:

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

This will match a leading term, containing no commas, followed by zero or more commas then non comma terms.

JavaScript code:

var inputs = ["orange", "apple, orange", "apple,     orange", "apple,orange", "apple, orange,", ",orange, apple", "orange,, apple", "apple,,orange"];
for (var i=0; i < inputs.length; ++i) {
    console.log(inputs[i] + " => " + /^[^,]+(?:\s*,\s*[^,]+)*$/.test(inputs[i]));
}

about 4 years ago · Juan Pablo Isaza Report

0

You could check if the string does not match this basic pattern:

^\s*,|,\s*,|,\s*$

See demo at regex101 or JS demo at tio.run

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!