skip to Main Content

The string can contain any number of any characters in any order. However, it should not contain both comma and dash simultaneously. Examples:

123 // ok
1,23 // ok
1-23 // ok
1,2-3 // not ok
1-2-3 // ok
foobar! // ok
a---t // ok
,3,*, // ok
k-, // not ok

2

Answers


  1. It looks like for searching a group with comma or dash and repeating of this group or not.

    /^d([,-])?d1?d$/
    
    Login or Signup to reply.
  2. Regex:

    /^(?!.*(?=.*-)(?=.*,)).*$/
    
    function checkCommaDash (str){
      /^(?!.*(?=.*-)(?=.*,)).*$/.test(str) ? console.log('ok') : console.log('not ok')
    }
    checkCommaDash('123')
    checkCommaDash('1,23')
    checkCommaDash('1-23')
    checkCommaDash('1,2-3')
    checkCommaDash('1-2-3')
    checkCommaDash('foobar!')
    checkCommaDash('a---t')
    checkCommaDash(',3,*,')
    checkCommaDash('k-,')
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search