Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Snow909
Tera Expert

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
Kilo Patron
Kilo 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
Kilo Patron
Kilo 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...