-1

I am a beginner when it comes to regex. I have string cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd,o=someOrg,c=UK. I need to get foo,bar and zoo so I used following regex to extract string in javascript.

const dn = 'cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd,o=someOrg,c=UK';
const regex = /^cn=(\w+),ou=(\w+),ou=(\w+)/;
const found = dn.match(regex);
console.log(found) --> Array ["cn=foo,ou=bar,ou=zoo", "foo", "bar", "zoo"]

Then ou=bar value is changed upon new requirement to ou=bar - 1. It could have - or numeric value in any order within that string value. I tried following regex.

const regex = /^cn=(\w+),ou=(.+),ou=(\w+)/;

however it returns unwanted data Array ["cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd", "foo", "bar,ou=zoo,ou=aa,ou=bb,ou=cc", "dd"]

What I expect is Array ["cn=foo,ou=bar - 1,ou=zoo", "foo", "bar - 1", "zoo"]. I tried to exclude unwanted data via ^(ou=aa|ou=bb|ou=cc|ou=dd|o=someOrg|c=UK) within the regex but I got null value. I'd appreciate someone can help me to correct regex syntax.

Update:

I tried /^cn=(\w+),ou=(\w+\s+-\s+\d+),ou=(\w+)/ but this covers it above example but it won't cover something like ou=bar-1 or ou=1bar-..

DaeYoung
  • 1,161
  • 6
  • 27
  • 59
  • 1
    I think you might want to use an existing library for parsing LDAP addresses rather than using a regex, especially as LDAP paths can contain special characters and escape sequences which are non-trivial to handle correctly with a regex: http://www.selfadsi.org/ldap-path.htm – Dai Nov 15 '19 at 00:06

1 Answers1

0

The easies way to solve this task is to make it non-greedy (notice the question mark!):

const regex = /^cn=(\w+),ou=(.+?),ou=(\w+)/;

Alternatively, you may want to exclude the comma:

const regex = /^cn=(\w+),ou=([^,]+),ou=(\w+)/;
AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
  • LDAP path values can contain commas and other special characters and its has its own escaping rules - so the latter idea might not work: http://www.selfadsi.org/ldap-path.htm – Dai Nov 15 '19 at 00:07