Reputation: 1
Indesign GREP newbie here. I'm receiving text of times that requires reformating. How would I format time ranges using a GREP code to omit the first "am" or "pm" from any time ranges that have "am" or "pm" twice in the same time range? Also, I'm trying to omit the ":00" from all time ranges. Is there a simple way of eliminating that in the same GREP?
For Example:
This is what’s provided: • 9:15am-10:00am • 1:00pm-3:30pm • 11:30am-12:30pm • 9:00am-1:00pm
This is what I’m aiming for: • 9:15-10am • 1-3:30pm • 11:30am-12:30pm • 9am-1pm
Upvotes: -1
Views: 140
Reputation: 6418
This can be done with GREP, but you will need 2 searches:
am(?=-\d{1,2}:\d{2}am)
pm(?=-\d{1,2}:\d{2}pm)
Upvotes: 0
Reputation: 14537
If you don't mind a script, here is the basic algorithm:
function clean_ranges(txt) {
txt = txt.replace(/:00/g, '');
var ranges = txt.split(' • ');
for (var i=0; i<ranges.length; i++) {
if (ranges[i].split('am').length>2) ranges[i] = ranges[i].replace('am','');
if (ranges[i].split('pm').length>2) ranges[i] = ranges[i].replace('pm','');
}
return ranges.join(' • ');
}
var str = '• 9:15am-10:00am • 1:00pm-3:30pm • 11:30am-12:30pm • 9:00am-1:00pm';
console.log(clean_ranges(str));
The full implementation for InDesign is here:
function clean_ranges(txt) {
txt = txt.replace(/:00/g, '');
var ranges = txt.split(' • ');
for (var i = 0; i < ranges.length; i++) {
if (ranges[i].split('am').length > 2) ranges[i] = ranges[i].replace('am', '');
if (ranges[i].split('pm').length > 2) ranges[i] = ranges[i].replace('pm', '');
}
return ranges.join(' • ');
}
try {
var txt = app.selection[0].contents;
if (txt.split('-').length < 2) exit(); // do nothing if selected text has no '-'
app.selection[0].contents = clean_ranges(txt);
} catch(e) {}
It will replace a selected text.
Upvotes: 1