0

I am not extremely well versed in Regular Expressions but I believe I have found a pattern that should work for my use case (tested on sites like regex101.com). My issue is that I am trying to find the index of the last occurrence of [1] within the String and insert a new line at its position. Here is the String (note: this is a single string with a newline character inserted before 'Set 2' and 'Encore'):

Set 1: Llama, Bouncing Around the Room, Divided Sky, Fee, It's Ice, My Sweet One > Guelah Papyrus, The Lizards, Foam, David Bowie[1] 
Set 2: Golgi Apparatus, The Squirming Coil, Brother, Sparkle, The Landlady > Destiny Unbound, Mike's Song > I Am Hydrogen > Weekapaug Groove, Lawn Boy > Chalk Dust Torture 
Encore: Memories[2], Poor Heart, Sweet Adeline[2][1] "Dr. Seuss" chants from Fish.[2] Without microphones.

And here is the RegEx pattern:

var pattern = /(\[1\])(?!.*\[1\])/g;

I then call the following two methods:

var match = pattern.exec(setlistFormatted);
var finalFormat = setlistFormatted.splice(match.index, 0, "\n");

The splice method here is a custom function I found on StackOverflow for my use case:

String.prototype.splice = function(idx, rem, str) {
    return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};

I'm having trouble recreating the exact issue, but in most cases the RegEx pattern returns the first occurrence of [1] instead of the last and outputs:

Set 1: Llama, Bouncing Around the Room, Divided Sky, Fee, It's Ice, My Sweet One > Guelah Papyrus, The Lizards, Foam, David Bowie
[1] 
Set 2: Golgi Apparatus, The Squirming Coil, Brother, Sparkle, The Landlady > Destiny Unbound, Mike's Song > I Am Hydrogen > Weekapaug Groove, Lawn Boy > Chalk Dust Torture 
Encore: Memories[2], Poor Heart, Sweet Adeline[2][1] "Dr. Seuss" chants from Fish.[2] Without microphones.

From other posts I've searched / read I've seen that Google Apps Script does not support certain functionality of Javascript's RegExp - mainly capture groups. So my questions are these:

  1. My pattern does return instances of [1]. Does this not count as a capture group? Clearly my code finds an instance of [1] and inserts a newline before it.
  2. If the pattern is able to capture instances of [1], why does my expression return all occurrences instead of the last? Calling Logger.log(match) returns [1],[1]

BACKGROUND: For those interested I am creating a GroupMe bot that posts Setlist data for the jam band Phish using the Phish.net API. In this particular case, everything following the last [1] represents the setlist notes and should be displayed on a new line instead of the same line as the Encore.

Alex
  • 51
  • 5
  • 1
    Looks like the only issue is the `.`, use `/(\[1\])(?![\s\S]*\1)/g` – Wiktor Stribiżew Sep 26 '17 at 19:45
  • Even I was about to post the exact same regex. [LINK](https://regex101.com/r/Sm0DGb/1) – Gurmanjot Singh Sep 26 '17 at 19:53
  • @WiktorStribiżew Thanks for your input! It looks like that pattern does return the index I'm looking for. However, I'm unable to insert a newline character before it. Regardless, this solves the main issue I was having. Thank you!!! – Alex Sep 26 '17 at 20:04

0 Answers0