简体   繁体   中英

How do I combine two javascript string.join with comma seperators?

Thanks to the help of Sigfried on here I was able to do a regex match on each line within a textarea and parse it out to a new textarea. Worked great but now I need to combine the two matches it pulls out with a comma in between, on each line.

So the following data from textarea (id= input ):

1234567812345678
8765432187654321

becomes this an is placed in a textarea (id= output ):

1234567812345678,123456
8765432187654321,876543

Currently the code parses 16 digits and puts it in output2 and then parses the first 6 digits only which go into output1 . Same string, two different outputs with different lengths. The actual data has a lot more than just 16 digits but I am trimming it down to 16 digits and then also 6 digits.

function trimit() {
    var str = "";
    str = document.getElementById("input").value;  
    var sixes = str.match(/(^......)/mg);
    var sixteens = str.match(/(^................)/mg);  
    document.getElementById("output1").value = sixes.join('\n');
    document.getElementById("output2").value = sixteens.join('\n');
}

I tried something simple like sixteens.join + "," + sixes.join('\\n'); and sixteens.join('\\n') + "," + sixes.join('\\n'); but they failed. One insert a function message and the other only combined the last line.

When I did this I got the following:

1234567812345678
,123456
8765432187654321
,876543

OR

1234567812345678
8765432187654321,123456
876543

instead of my example above with the combined strings in one line, line by line.

Help oh wise ones!

Not sure if i understand correctly but if you neef the final output to only have ',' , then why do you call join with '\\n'. Try sixteens.join(',') + "," + sixes.join(',');

, is the default separator of join anyways, so you can call join() also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM