indexOf not working as expected?

gokulraj
Giga Expert

How to compare the string with exact match?

myString = "networkgroup@abc.com";

"network@abc.com" also satifying this condtion

Is there a way to find the exact string insted of indexOF()

16 REPLIES 16

Hi,

if you are sure the value you want to compare will be present exactly then you can directly use double equal to opeartor

if(comment == myString)

Regards
Ankur

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

but the comment not only contains  the item name, it will be like below

comments = 'EVMTMW-WOZ' item not found/n 'TMAQW-WOZ' does not exist

myString = 'EVMTMW' (Queried from item name table hold's the item name and need to compare with the comment)

Hi,

then I believe you need to use indexOf only as your mystring can be present at any place

Regards
Ankur

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

Allen Andreas
Administrator
Administrator

Hello,

How is JavaScript's IndexOf "not working as expected"?

Anyways, there's regex and I believe another method, but it's a bit more complicated when this whole thing may need to be. You should be fine using IndexOf if used properly:

https://www.w3schools.com/jsref/jsref_indexof.asp

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Aman Singh_
Kilo Sage

Hi,

 

Try using 

.startsWith("<field>")

var emails='networkgroup@abc.com';
if (emails.startsWith("network@abc.com"))
{

gs.print('exact');
}
else
{
gs.print('not exact');
}

Tried giving me Not exact.

Please mark Correct✅/helpful???? if applicable, thanks!!

Aman