- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:26 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:48 AM
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:
-
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'); }
-
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', andfalse
otherwise. Choose the method that best fits your requirements and coding style.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:52 AM - edited 02-13-2024 04:55 AM
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();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:57 AM
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
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 05:02 AM
Hi @gunishi
Below post could be helpful to you :
https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:48 AM
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:
-
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'); }
-
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', andfalse
otherwise. Choose the method that best fits your requirements and coding style.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:52 AM - edited 02-13-2024 04:55 AM
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();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 04:57 AM
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
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 05:02 AM
Hi @gunishi
Below post could be helpful to you :
https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172
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.