1

I have a working template (Google Doc) and have variables with following patterns to be replace with values

{{BASIC SALARY_.Description}}
{{OT1.5.Description}}
{{MEL ALW.Description}}
{{OST ALW.Description}}
{{TRV ALW.Description}}
{{ADV SAL.Description}}

note: I am using soft line break (ctrl+enter) in google doc as I couldn't figure out to detect normal linebreak pattern "\n", "\n", "\r\n" but my result always weird as some line need to be replaced as proper descriptions but some need to be totally nullify (remove whole {{pattern}} together with the line break to avoid empty line)

I have tried out multiple REGEX patterns, googled the online forum https://github.com/google/re2/wiki/Syntax
Eliminate newlines in google app script using regex
Use RegEx in Google Doc Apps Script to Replace Text

and figure out only soft linebreak is the only way to deal with (identify pattern \v. Please check my sample code as the pattern replace doesn't work as expected.

// code block 1
var doc = DocumentApp.openById(flPayslip.getId());
var body = doc.getBody();
body.replaceText("{{BASIC SALARY_.Description}}", "Basic Salary");
body.replaceText("{{OST ALW.Description}}", "Outstation Allowance");
// code block 2
var doc = DocumentApp.openById(flPayslip.getId());
var body = doc.getBody();
body.replaceText("{{BASIC SALARY_.Description}}", "Basic Salary");
body.replaceText("{{OST ALW.Description}}", "Outstation Allowance");
body.replaceText("{{.*}}\\v+", "");  // to replace soft linebreak

Actual Result of code block 1

Basic Salary
{{OT1.5.Description}}
{{MEL ALW.Description}}
Outstation Allowance
{{TRV ALW.Description}}
{{ADV SAL.Description}}

Actual Result of code block 2:

Basic Salary

Issue: actual result "Outstation Allowance" was removed from regex replacement.

Expected result

Basic Salary
Outstation Allowance

What's the proper regex pattern I should use in my code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Weilies
  • 500
  • 1
  • 7
  • 27
  • Can I ask you about your question? In your actual situation, for example, is ``{{BASIC SALARY_.Description}}`` enclosed by some texts? Or the values of ``{{###}}`` are not enclosed by texts, and each value is only put in a paragraph? In order to correctly understand your situation, can you provide a sample Document you want to use? Of course, please remove your personal information. – Tanaike May 16 '19 at 05:26
  • here is my actual template file https://docs.google.com/document/d/1-8sOxKeLUpkYq8oymKF7OT3LyX2bA2vyIcvvhX08sCE/edit?usp=sharing and to it's a simplified ver. my expectation was for certain item (e.g OT, it include unit), and it become {{OT1.5.Description}} ({{OT1.5.Unit}}), and the replaced result is "OT1.5 (3)" instead of just "OT1.5". and it would be great if can dynamically replace with "unit" for certain items! :) – Weilies May 16 '19 at 05:47
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike May 16 '19 at 12:43

1 Answers1

2

Try

body.replaceText("{{[^\\v]*?}}\\v+", "");  // No \v inside `{{}}` and  not greedy`?`

When you use {{.*}}, .* matches everything between the first {{ and the last }}

Basic Salary
{{

OT1.5.Description}}
{{MEL ALW.Description}}
Outstation Allowance
{{TRV ALW.Description}}
{{ADV SAL.Description

}}

Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • it worked like MAGIC!!! Thanks @TheMaster ! thanks for the magic code and i still trying to understand it. If i wanna make my template more meaningful, some earning items need to display unit, e.g. _{{OT1.5.Description}} ({{OT1.5.Unit}})_ it doesn't work. Here is my template https://drive.google.com/open?id=1-8sOxKeLUpkYq8oymKF7OT3LyX2bA2vyIcvvhX08sCE Thanks for the helps again! – Weilies May 16 '19 at 06:16
  • @Weilies Kindly ask a well researched new question like this. – TheMaster May 16 '19 at 06:35
  • sorry, correct me if i am wrong. should i create a separate thread with small vary of my question? anyway, i found a way to get rid of the additional changes. by adding the line below, right after your inspiring code and it worked. **body.replaceText("{{[^\\v]*?}} \\({{[^\\v]*?}}\\)\\v+", "");** – Weilies May 16 '19 at 06:38
  • 1
    @Weilies Yes, but only focused on the new problem. As it is, I don't understand your new problem. And I don't want to visit external links. – TheMaster May 16 '19 at 06:46
  • 1
    @Tanaike Thanks. – TheMaster May 16 '19 at 13:06