Regex match Google Scripts

1,918 views
Skip to first unread message

jmm0979

unread,
Mar 24, 2021, 7:55:42 PM3/24/21
to google-apps-sc...@googlegroups.com
Hi,
I am trying to see if someone can help me on how to use the regex match in GAS.

I need to remove the following characters if they exist in the beginning of a string and replace them with "New - ".

string contains the following
**-
%% ABC -

For example:
**-Apple
%% ABC - Orange

Results:
New - Apple
New - Orange

var col1 = first.replace(/^**--|^%% ABC - /,"");
      col1 = col1.toString().replace(/\s+/g, " ").trim(); // remove extra trailing white spaces
      col1 = col1.replace(/\b[a-z]/ig, function(match) {return match.toUpperCase()})

TIA!
JMR

Tanaike

unread,
Mar 24, 2021, 11:15:26 PM3/24/21
to Google Apps Script Community
Although I'm not sure whether I could correctly understand about your question, when you want to achieve as follows.

From:
**-Apple
%% ABC - Orange

To:
New - Apple
New - Orange

How about the following sample script?

const res = ["New - Apple", "New - Orange"].map(e => e.replace(/^\*\*\-\-|^%% ABC \- /, "New - "));
console.log(res) // [ 'New - Apple', 'New - Orange' ]

If I misunderstood your question, I apologize for this.

jmm0979

unread,
Mar 25, 2021, 5:23:08 PM3/25/21
to google-apps-sc...@googlegroups.com
Hi Tanaike,

I am using this code:
var col1 = first.replace(/^(**-)|^(!!-ABC - )|^($$-ABC - )/,"New - ");

But when the return value only displays "ABC - ", it did not include the remaining text value.

I am hoping for the return to be like this:
Screen Shot 2021-03-25 at 2.20.58 PM.png


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/94db3ecd-1eba-47a6-826c-12d50b8757a0n%40googlegroups.com.

Tanaike

unread,
Mar 25, 2021, 8:03:34 PM3/25/21
to Google Apps Script Community
Thank you for replying. I apologize for the inconvenience. In that case, how about the following sample script?

const res = ["**-Apple", "!!-ABC - Orange", "@@- Mango", "Mango"].map(e => `New - ${e.split("-").pop().trim()}`);
console.log(res) // [ 'New - Apple', 'New - Orange', 'New - Mango', 'New - Mango' ]

Bruce Mcpherson

unread,
Mar 26, 2021, 6:25:36 AM3/26/21
to google-apps-sc...@googlegroups.com
If it's anything before the last '-',

then str.replace(/.*-+/, '"New -")  will do it


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages