user15131352
user15131352

Reputation:

how to convert the first output in second output in javascript?

i have this javascript variable with this content: i must convert the "," in "newline" to have multiple lines. I am thinking about regExp,but i don't know how to manage it.

var output= "Query Response #1:,(0008,0005) CS ssxssx,(00,00) DA dsdww,(w,e) TM dww,(20)sxs";

i would want to have an output of this type:

Query Response #1:
(0008,0005) CS ssxssx
(00,00) DA dsdww
(w,e) TM dww
(20)sxs

Does anyone have any ideas to fix this problem? Thanks

this is the regExp that matches those "," but i don't know how to reproduce the desidere output

var regex_virgola= /\,\(/gm;

var regex_virgola= /\,\(/gm;
regex_virgola= regex_virgola.toString();

function getIndexes(searchStr, str) {
    var searchStrLen = searchStr.length;
    if (searchStrLen == 0) {
        return [];
    }
    var startIndex = 0, index, indices = [];
    str = str.toLowerCase();
    searchStr = searchStr.toLowerCase();
    while ((index = str.indexOf(searchStr, startIndex)) > -1) {
        indices.push(index);
        startIndex = index + searchStrLen;
    }
    return indices;
}

var Indici  = getIndexes(regex_virgola, output);
console.log('IndiciVirgola:'+ Indici); 

BUT IN CONSOLE IT GIVES TO ME EMPTY STRUCTURE

Upvotes: 0

Views: 65

Answers (1)

Aalexander
Aalexander

Reputation: 5004

With the regex you can use the .split() method of string which will result in an array. To create the output you can join() the array with \n to create the line breaks.

var output = "Query Response #1:,(0008,0005) CS ssxssx,(00,00) DA dsdww,(w,e) TM dww,(20)sxs";


var regex_virgola = /\,\(/gm;

const res = output.split(regex_virgola);
console.log(res.join("\n("));
Query Response #1: (0008, 0005) CS ssxssx (00, 00) DA dsdww (w, e) TM dww (20)sxs

Upvotes: 1

Related Questions