Please provide me the regex for extracting CN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
57m ago
Hi,
I am working with a field that contains LDAP Distinguished Names (DNs) concatenated using ^ as a separator. I need to extract the CN (Common Name) from each DN. CNs can be either numeric IDs (e.g., 02042174) or person names that may include escaped commas (e.g., DUNN\, KAREN {FLNA}).
Sample input (single string)
CN=DUNN\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt..........................
Expected CN outputs: I need to extract CN from all DN in the below format.
DUNN, KAREN {FLNA}
King, Alzra {FLNA}
02042174
Wilson, Brian J {FLNA}
I have written this script but not showing result as expected :
var members = "CN=DUNN\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var dnArray = members.split('^');
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Extract CN value
var cnMatch = dn.match(/CN=([^,]+)/);
if (!cnMatch) continue;
var cnValue = cnMatch[1].replace("\\,", ",").trim();
gs.info("Chandra@@@: CNmatch: " + cnValue);
}
Please correct this where needed.
Thank You in advanced.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
34m ago - last edited 25m ago
Hi @Chandra18,
Try this script:
var members = "CN=DUNN\\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var dnArray = members.split('^');
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Capture CN including escaped commas
var cnMatch = dn.match(/CN=((?:\\.|[^,])+)/);
if (!cnMatch) continue;
// Replace escaped commas
var cnValue = cnMatch[1].replace(/\\,/g, ",").trim();
gs.info("Chandra@@@: CNmatch: " + cnValue);
}
Result:
If you find this answer useful, please mark it as solution accepted/helpful and close the thread.
Thanks and Regards,
Mohammed Zakir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
22m ago
try this
var members = "CN=DUNN\\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var dnArray = members.split('^');
var cns = [];
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Extract CN by splitting on comma and taking the first part
var cnPart = dn.split(',')[0];
var cnValue = cnPart.replace('CN=', '').replace(/\\,/g, ',').trim();
cns.push(cnValue);
gs.info("CN: " + cnValue);
}
gs.info("All CNs: " + cns.join(", "));
Output:
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
