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-
..