Please provide me the regex for extracting CN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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
2 hours ago
Hi @Chandra18 ,
You can use the below logic which can give u the expected result.
var inputString = "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 cnArray = [];
var regex = /CN=((?:\\,|[^,])*)(?=,OU=|$)/g;
var match;
while ((match = regex.exec(inputString)) !== null) {
var cnValue = match[1]
.replace(/\\,/g, ',') // unescape commas
.trim();
cnArray.push(cnValue);
}
gs.info("Extracted CNs:\n" + cnArray.join("\n"));
If you find this answer useful, please mark it as solution accepted/helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @sunilsargam
in testing why are you taking two backslash (\\,) but in my field value it is single one (\,)
CN=DUNN\\, KAREN {FLNA}it should work for with single backslash .
CN=DUNN\, KAREN {FLNA}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
50m ago
Hey @Chandra18 ,
In JavaScript/ServiceNow script, a single backslash \ is an escape character inside string literals and regex literals.
So to represent a literal backslash in code, you must write \\.
Try to execute the above script with your dynamic output.
Let me know if this works or not
Thank you
Sunil Sargam
