Search code examples
google-apps-scriptgmail

SCRIPT GMAIL : Import only the text in parentheses in subject


I want to create en event from a specific mail I'm receiving. Inside the subject of the mail, inside parenthese there is the date for the event.

I try to import the specific text contained in the subject of the gmail message.

The global subject is : EDF - Tempo : demain jour ROUGE (mercredi 10 janvier 2024) I just want to import the text inside the parentheses id est: mercredi 10 janvier 2024

I don't know how to do that

I try to use :

subject.split("(")

But the end parenthese comes with


Solution

  • You can use RegEx to find text within parentheses. The pattern /\(.+\)/ will just grab all text from the first ( until the last ) but it will work for the provided example.

    function getTextInParentheses(text) {
      return /\(.+\)/.exec(text)[0].slice(1, -1) // Return inner text
    }
    
    const subject = "EDF - Tempo : demain jour ROUGE (mercredi 10 janvier 2024)"
    const date = getTextInParentheses(subject)
    
    console.log(date) // mercredi 10 janvier 2024