indexOf doesn't match the exact string?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2018 04:08 AM
Hi team,
I have a requirement to get the exact string comparison results.
if my condition is like [ if (emailTo.indexOf("networkoperation@test.com") != -1) ]
it will also satisfy the conditions if the email To contains like the following("operation@test.com")
How to find the exact string matching?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2018 09:13 AM
If you are looking for an exact match then you can simply check like :
if (emailTo=="networkoperation@test.com")

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2018 04:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2018 08:01 AM
I'm not 100% sure what it is you are asking, but you could use the native array.indexOf method to easily look for a matching email address in an array:
var emails = 'networkoperation@test.com, operation@test.com';
var emailArray = emails.toLowerCase().replace(/\s+/g, '').split(","); //create an array from the comma-separated string, forced to lowercase first
gs.info(emailArray.indexOf("operation@test.com") > -1);
...should return "true" for you.
If running in an instance before Helsinki (I'm assuming not), you can also use the "contains" method in the ArrayUtil Script Include:
var emails = 'networkoperation@test.com, operation@test.com';
var emailArray = emails.toLowerCase().replace(/\s+/g, '').split(","); //create an array from the comma-separated string, stripping all spaces and forcing to lowercase first
var arrayUtil = new ArrayUtil();
gs.info(arrayUtil.contains(emailArray, "operation@test.com"));
...would return "true" as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2018 09:04 AM
Hey Jim,
Like you said array.indexOf method will give the match,but some cases it fails,
For E.g,
var emails='networkoperation@test.com ';
if (emails.indexOf("operation@test.com")>-1)
{
gs.print('hello');
}
In this case it will match the if condition,
So to avoid this I want to match the exact string..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2018 08:22 AM
You may want to try looping through the email array and comparing the emails that way:
var emails = 'networkoperation@test.com, operation@test.com';
var emailArray = emails.toLowerCase().replace(/\s+/g, '').split(",");
for(var i = 0; i < emailArray.length; i++){
if(emailArray[i] == 'networkoperation@test.com'){
//do something if they match
}
}