The CreatorCon Call for Content is officially open! Get started here.

How to split a string and extract substring?

Sid_Takali
Kilo Patron

Hi all,

I have a requirement where I have to split a string  "Call Inbound from 01037669762  - ConnectionId: e7541016-1a5a-4c24-9375-0a3f8793e6cc"  and I have to extract a phone number(substring) "01037669762" and populate on consumer phone field.

find_real_file.png

9 REPLIES 9

Hi Sagar,

One correcttion here, when you use getValue() , that already returns string equivalent of the value, you don't need to convert it string explicitly

Best Regards
Aman Kumar

Thanks @Aman Kumar!

 

ok. understood.

 

if we get the values like

object.short_description.toString();

then, we have to use toString() method.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Siddharam Takali,

Good day!

Thanks for marking helpful! If my answer help you. Feel free to mark correct!

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Actually that is not correct in all cases: if object is a GlideRecord and the field has no value. Then the returned value is null, not a string. Of course, in that case even toString() is not a solution, as stringifying a null will not yield an empty string.

-O-
Kilo Patron

Here's a variant that uses RegExp, looking for the word from followed by a number:

var regexp = /from\s(\d+)\s/;
var findings = regexp.exec('' + gr.short_description);

if (findings)
	gr.consumer_phone = findings[1];

This assumes the script will be executed server side and gr is the interaction GlideRecord. On client side it would be mostly the same, just that gr has to be substituted using g_form calls:

var regexp = /from\s(\d+)\s/;
var findings = regexp.exec(g_form.getValue('short_description'));

if (findings)
	g_form.setValue('consumer_phone', findings[1]);