D
Doris
A list of points, defined by x and y coordinates, is to reversed. The list
may be three to five points long. So for example
list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
I am looking for a regular expression which will capture all these possible
lengths in one pattern, but am having trouble removing and adding back in
the commas. I can do it with three calls to the replace method, but there
must be a better, more flexible and elegant solution:
newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
'$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
'$4,$3,$2,$1' ).replace(
/^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/, '$5,$4,$3,$2,$1' );
Any hints?
Thanks,
Doris
may be three to five points long. So for example
list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
I am looking for a regular expression which will capture all these possible
lengths in one pattern, but am having trouble removing and adding back in
the commas. I can do it with three calls to the replace method, but there
must be a better, more flexible and elegant solution:
newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
'$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
'$4,$3,$2,$1' ).replace(
/^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/, '$5,$4,$3,$2,$1' );
Any hints?
Thanks,
Doris