Enter Case number reference for associated cases.

Case #


Type: APM as a Service

Old Article ID: 1516

Old Article Type: howto


Detail the contextual information specific to the issue; i.e. Product, Version, Agent, System, etc.

  • Dynatrace Recorder

You can use this JavaScript for advanced or custom scripting scenarios.

One possible implementation, shown in the example below, would be to use this function to parse out a value from a specific location in a list; for example, the user selects the 5th list element.

The indexOf() JavaScript function finds the first instance of a given substring in a longer string. But what if you want to find the second, or third instance instead?

The JavaScript function below will parse a string and return the position of the Nth instance of a given substing in that parent string.


 /** Function Definition **/
function nthChar(str, chr, n){
 var count = 0;
 var currIndex = 0;
 var index; 
 while(count < n){
 if (count == 0) {
 index = str.indexOf(chr);
 currIndex += index;
 alert('currIndex:' + currIndex);
 } else {
 index = str.indexOf(chr);
 currIndex += (index+1);
 alert('currIndex:' + currIndex);
 str = str.slice(index + 1);
 alert('str = ' + str);
 return currIndex;
 /** Function Implementation **/
/* myString is the string we need to search through */
var myString = 'text1,text2,text3,text4,text5,text6,text7';
 let's say we want to pull the fifth list value (located between the 4th
 and 5th commas... this is how we do it using this function**/
var fourthComma = nthChar(myString, ¿æ¿¿¬¿¿,¿æ¿¿¬¿¿¢, 4); // position of 4th comma
var fifthComma = nthChar(myString, ¿æ¿¿¬¿¿,¿æ¿¿¬¿¿¢, 5); // position of 5th comma
/*slice out the value between the fourth comma and fifth comma **/
var fifthValue = myString.slice(fourthComma+1, fifthComma);
alert(fifthValue); // should alert back with 'text5'