- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:01 AM
I need to extract the user name from an AD attribute.
The string looks something like "CN=Rosana Smith,OU=Employees,..."
I wrote an expression of: var mike = str.match(/\b(\w)+ (\w)+/jg);
This works, except if I run into a name of Rosana Smith-Jones, it will not capture the third name. If I adjust to match against this then it doesn't work for non-hyphen names. Also, some people have 3 names "Billy Rae Cyrus".
My question is, how can I write an expression to capture all characters between (but not including) the CN= and the first comma (,) ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:43 AM
Here is how you would get the name out of the string:
var myString = "CN=Rosana Smith,OU=Employees,..";
var myRegexp = /CN=(.*?),/g;
var match = myRegexp.exec(myString);
gs.print(match[1]);
The idea is you want to retrieve the first matching group and not the match itself. Run it in a Background Script or in Xplore: Developer Toolkit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:18 AM
If I enter your code as follows as a background script:
var str = ‘CN=Rosana Smith,OU=Employees,OU=Users,OU=Buenos_Aires,OU=Argentina,‘;
var mySubString = str.substring(str.lastIndexOf("=") +1, str.lastIndexOf(","));
gs.print(mySubString);
I get this output (which is incorrect) - I need the person's name:
*** Script: Argentina
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:21 AM
OK, your idea got my on the right path.
This works:
var mySubString = str.substring(str.indexOf("=") +1, str.indexOf(","));
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:29 AM
glad that it worked. Please mark the correct answer whichever you think is useful, so the thread can be completed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 06:47 AM
I would look at Jim's answer from a couple minutes ago, it is much better way to extract the string