Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to check part of a string

gunishi
Tera Guru

Hi there, 

 

This is more of a basic question, but I would like to check the value in the string for a certain combination of letters, let's say 'ABC'.

 

How would I return true if the field contains ABC-234, and I am only checking to see if it contains ABC.

 

I know I could do this via a filter, but in script, how would be best to do this? Just via an encoded query?

Many thanks!

G

4 ACCEPTED SOLUTIONS

Ratnakar7
Mega Sage

Hi @gunishi ,

In script, you can check if a string contains a specific substring using the indexOf() method or a regular expression. Here's how you can do it:

  1. Using indexOf(): You can use the indexOf() method to check if a string contains a specific substring. If the method returns a value greater than or equal to 0, it means the substring is present in the string.

    var str = 'ABC-234';
    var substring = 'ABC';
    
    if (str.indexOf(substring) !== -1) {
        // Substring found in the string
        gs.info('Substring found');
    } else {
        // Substring not found in the string
        gs.info('Substring not found');
    }
    

     

  2. Using Regular Expression: You can also use a regular expression to achieve the same result. Regular

    var str = 'ABC-234';
    var regex = /ABC/;
    
    if (regex.test(str)) {
        // Substring found in the string
        gs.info('Substring found');
    } else {
        // Substring not found in the string
        gs.info('Substring not found');
    }
    

    Both of these methods will return true if the substring 'ABC' is found in the string 'ABC-234', and false otherwise. Choose the method that best fits your requirements and coding style.

 

Thanks,

Ratnakar

View solution in original post

Sarika S Nair1
Kilo Sage

Hi @gunishi  

Below script can be used in BR. 

If(current.<stringfieldname>.indexOf('ABC')>=0){

gs.addInfoMessage('ABC Present');

}

And below script can be used in client script 

var stringfieldname = g_form.getValue('<stringfield>');

if(stringfieldname.indexOf('ABC')>=0){

alert('ABC PRESENT');

}

 

OR if you are trying to use it in GlideQuery then you can use below 

var grRec = new GlideRecord('tablename');

grRec.addEncodedQuery('stringfieldnameLIKEABC');

grRec.query();

 

View solution in original post

Aman Kumar S
Kilo Patron

Hi @gunishi 

Apart from what Ratnakar has suggested, you can also try using includes function:

var str = "abc123";
var textFound = str.includes("123");// this would return true

Best Regards
Aman Kumar

View solution in original post

Amit Verma
Kilo Patron
Kilo Patron

Hi @gunishi 

 

Below post could be helpful to you :

https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172

https://www.servicenow.com/community/developer-articles/servicenow-javascript-tutorials-string-funct...

 

The easiest way to do it to make use of indexOf() method.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

gunishi
Tera Guru

Hi @Amit Verma @Aman Kumar S @Sarika S Nair1 @Ratnakar7 

 

Thank you all so much for your responses and all good solutions!

Many thanks again. 

 

G