We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Please provide me the regex for extracting CN

Chandra18
Mega Sage

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.

1 ACCEPTED SOLUTION

@Chandra18 

try this

function extractCNs(members) {
    var cns = [];
    var dnArray = members.split('^');
    
    for (var i = 0; i < dnArray.length; i++) {
        var dn = dnArray[i].trim();
        if (!dn) continue;

        // Match CN=... until the next comma that is not part of the CN value
        var match = dn.match(/^CN=([^,]+(?:,[^=]+)*)/);
        if (match) {
            var cnValue = match[1].replace(/\\,/g, ',').trim();
            cns.push(cnValue.replace(",OU", ''));
        }
    }
    
    return cns;
}

// Example usage
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 cns = extractCNs(members);

// Output each CN on a new line
for (var i = 0; i < cns.length; i++) {
    gs.info(cns[i]);
}

Output:

AnkurBawiskar_0-1765357089986.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

17 REPLIES 17

@Chandra18 

yes why not

Thank you for marking my response as helpful.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 
Thank You for your support.
Now result coming like : CN=BALLEZ, ANTONIO {FLNA}.
I want add one more space before {FLNA} like : CN=BALLEZ, ANTONIO   {FLNA}

Please help on this I will accept as solution.

@Chandra18 

try this

function extractCNs(members) {
    var cns = [];
    var dnArray = members.split('^');
    
    for (var i = 0; i < dnArray.length; i++) {
        var dn = dnArray[i].trim();
        if (!dn) continue;

        // Match CN=... until the next comma that is not part of the CN value
        var match = dn.match(/^CN=([^,]+(?:,[^=]+)*)/);
        if (match) {
            var cnValue = match[1].replace(/\\,/g, ',').trim();
            // Add an extra space before {FLNA}
            cnValue = cnValue.replace(/\{FLNA\}/, ' {FLNA}');
            cns.push(cnValue.replace(",OU", ''));
        }
    }
    
    return cns;
}

// Example usage
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 cns = extractCNs(members);

// Output each CN on a new line
for (var i = 0; i < cns.length; i++) {
    gs.info(cns[i]);
}

Output: 2 space before FLNA

AnkurBawiskar_0-1765368505664.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,
It is passing correctly before in scripts - background result also .
But I am using this in transform map it is showing only one space however, I used you latest script which is adding one more space.



@Chandra18 
Glad to know.

Please mark my response as correct and close the thread.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader