Search code examples
regexgoogle-apps-scriptgoogle-docs

Google Documents - remove redundant space charachters at paragraph endings


I am trying to have a GAS that will remove redundant space characters from paragraphs endings.

How can it be achieved?

Example:

This is sentence 1 in paragraph 1. This is sentence 2 in paragraph 1 
This is paragraph 2 (more text...)

There is a space character after "...sentence 2 in paragraph 1". (i.e. "...sentence 2 in paragraph 1". I want to get rid of it by using code.

So far I tried:

    paragraphs[i].replaceText(/\h\r/g,"\r");

where paragraphs[i] is the paragraph I'm checking (looping through all paragraphs in the document's body).

It gives this error:

Exception: Invalid regular expression pattern /\h\r/g

I would admit I have very basic knowledge in regex.


Solution

  • Try

    paragraphs[i].replaceText("\\s+$","");// double `\\` is intentional
    
    • \s: space at
    • $: end of string