0

I'm a bit stucked with this regex problem, I need to check if url has at start mongodb:// and at the end of a string - port with numbers :27017 and also if there is no slash / .

For example:

/myRegexForThis/.test('mongodb://testUrl:port') => true
/myRegexForThis/.test('mongodb://testUrl:port/') => false
/myRegexForThis/.test('mongo://testUrl') => false
/myRegexForThis/.test('mongodb://testUrl:port') => false

currently I have only start part: ^(mongodb:\/\/)

Kosmetika
  • 19,249
  • 37
  • 94
  • 154
  • There shouldn't be any slash in "testUrl" ? Also do you want to check if the URL is valid ? – HamZa Jun 02 '13 at 11:54
  • url can be like ``user:pass@example.com`` but as well as simple ``localhost`` – Kosmetika Jun 02 '13 at 11:57
  • 1
    So you do want to validate if the URL is valid ? Since if you don't want to you could simply use `^mongodb:\/\/.*?:\d+$`. – HamZa Jun 02 '13 at 11:58
  • yeah, i think there's no need to check, maybe only if it's not containing cyrrilic letters – Kosmetika Jun 02 '13 at 11:59
  • Does the "testUrl" string start with http(s) ? or is it just an IP/domain ? – HamZa Jun 02 '13 at 12:03
  • @HamZa url can be like user:pass@example.com but as well as simple localhost, no https – Kosmetika Jun 02 '13 at 12:04
  • 1
    Unfortunately a mongodb resource URI can be a lot more complex than that – Sammaye Jun 02 '13 at 12:15
  • 1
    Here we [goooooooo](http://regex101.com/r/vB8gL0), credits goes to [this answer](http://stackoverflow.com/a/2015516/) ! – HamZa Jun 02 '13 at 12:20
  • 2
    Example line 1 and 4, exactly the same, but different result? – Qtax Jun 02 '13 at 13:28
  • 1
    For an example mongo uri parser in js: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/url_parser.js – Ross Jun 25 '13 at 14:46

0 Answers0