Keyboard Maestro lets us get at parts of a string by their index number, where the parts are split on a custom delimiter:
( See the section on custom delimiters in manual:Variable Arrays [Keyboard Maestro Wiki] )
Here is a subroutine which lets us get at a continuous range (or 'slice') of zero-indexed parts of a string.
Variable Array SLICES Macros.kmmacros (5.7 KB)
For example:
If in the following long URL (see: Shorten Home Depot URL):
https://www.homedepot.com/p/1-2-in-x-5-ft-Conduit-Electrical-Metallic-Tubing-Steel-0550005000/202068069
we want to drop one part in the middle:
1-2-in-x-5-ft-Conduit-Electrical-Metallic-Tubing-Steel-0550005000
and retain just the preceding and following text, leaving 4 (/-delimited) parts from the beginning, and 1 part from the end:
https://www.homedepot.com/p/202068069
we could refer to the first four parts, starting at index 0, as 0,4
and refer to the last part with a negative index, counting back from the end, as -1
( These are the slice index conventions used by JavaScript, see Array.prototype.slice() - JavaScript | MDN )
Where more than one slice is wanted, slice arguments are separated by white space.
Expand disclosure triangle to view JS source of subroutine
const
delimiter = kmvar.local_Custom_delimiter,
parts = kmvar.local_Source_string.split(
delimiter
);
return kmvar.local_Slice_indices.split(/\s+/u)
.map(
sliceString => sliceString.split(",")
.map(Number)
)
.flatMap(
sliceIndices => parts.slice(...sliceIndices)
)
.join(delimiter);