1

How do I use regex in google apps script to replace the special characters in my text but only those between certain strings?

so if this was the text and x represents random alphanumeric characters...

xx@@xxxSTARTxxx@@@xxx$xxxENDxxxxx@@££xxxSTARTxxxx££££xxx&&&&&xxxxENDxxx

what regex would i need so i end up with

xx@@xxxSTARTxxxxxxxxxENDxxxxx@@££xxxSTARTxxxxxxxxxxxENDxxx

  • Please share the code you are using. – Wiktor Stribiżew Dec 17 '16 at 14:05
  • I would split this task into two: 1) Extract parts between START and END, 2) Throw away disallowed chars from the extracted parts, as follows: `s{\Q$start\E\K(.*?)(\Q$end\E)}{join "", $1 =~ /([a-zA-Z0-9]+)/g, $2}ge` (not tested) – Matthias Dec 17 '16 at 14:20
  • @Matthias: OP is using google apps script. Perl regex won't work there, it only supports JavaScript regex flavor. – Wiktor Stribiżew Dec 17 '16 at 14:22
  • Oh sorry then. Also I just tested the regex and the nested one overwrites the value of $2, so a working perl regex would be: `s{\Q$start\E\K(.*?)(\Q$end\E)}{join "", $1 =~ /([a-zA-Z0-9]+)/g, $end}ge`. However, the initial idea to split the problem into two should be transferable to JavaScript. – Matthias Dec 17 '16 at 14:26

1 Answers1

0

You may use a replace with a callback:

var text = "xx@@xxxSTARTxxx@@@xxx$xxxENDxxxxx@@££xxxSTARTxxxx££££xxx&&&&&xxxxENDxxx";
var regex = /(START)([\s\S]*?)(END)/g;
var result = text.replace(regex, function ($0, $1, $2, $3) {
    return $1 + $2.replace(/[^\w\s]+/g, '') + $3;
});
console.log(result);
// => xx@@xxxSTARTxxxxxxxxxENDxxxxx@@££xxxSTARTxxxxxxxxxxxENDxxx

The first regex is a simple regex to match a string between two strings:

  • (START) - Group 1 ($1): START (may be replaced with any pattern)
  • ([\s\S]*?) - Group 2 ($2): any 0+ chars, but as few as possible
  • (END) - Group 3 ($3): END (may be replaced with any pattern)

The regex to match special chars I used here is [^\w\s], it matches any 1+ chars other than ASCII letters, digits, _ and whitespaces.

See Check for special characters in string for more variations of the special char regex.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563