Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How to get the complete second occurence of the string separated by . using javascript

Not applicable

Hello All,

 

I need a help where i want to get the first half and second half of a value from a string from an output.

Example:

 I have a variable name= abc.hgl.knl;

i want the output as abc_need_to change.hgl.knl where i need to insert a custom string inbewteen the abc and hgl.

 

Any helpful answers will be appreciated.

Thanks

Sai

 

1 ACCEPTED SOLUTION

Brad Bowman
Mega Patron

If you mean the variable has the value of abc.hgl.knl and you want to change the value to something like abc_need_to change.hgl.knl, strictly following this format you can split on the period, append the first element, then rejoin:

var myString = 'abc.hgl.knl';
var myNewString = '_need_to change';
var myStringArr = myString.split('.');
myStringArr[0] += myNewString;
myString = myStringArr.join('.');
gs.print(myString);

You can combine a few of these lines - I showed each to make it easier to follow...

 

View solution in original post

1 REPLY 1

Brad Bowman
Mega Patron

If you mean the variable has the value of abc.hgl.knl and you want to change the value to something like abc_need_to change.hgl.knl, strictly following this format you can split on the period, append the first element, then rejoin:

var myString = 'abc.hgl.knl';
var myNewString = '_need_to change';
var myStringArr = myString.split('.');
myStringArr[0] += myNewString;
myString = myStringArr.join('.');
gs.print(myString);

You can combine a few of these lines - I showed each to make it easier to follow...