Searching for a substring with a newline using the "contains" function

I have something like the following in a Github Actions workflow:

on:
  issue_comment:
    types: [created]

jobs:
  job:
    name: My job
    if: startsWith(github.event.comment.body, 'foo') || contains(github.event.comment.body, '\nfoo')

The intention is that the job only runs if a comment is posted that contains “foo” at the start of a line. However, the second clause never seems to fire. I suspect this is because ‘\n’ is not being interpreted as a newline character. Are special characters in literals escaped in a different way?

How should I rewrite my workflow so that the job will run when I post a comment like:

test
foo

@bryangingechen ,

You can set to return the value of  github.event.comment.body as JSON-type  via the toJson function. In the JSON-type, the line-break will be converted to ’ \r\n’.

if: startsWith(github.event.comment.body, 'foo') || contains(toJson(github.event.comment.body), '\r\nfoo')
1 Like

Thank you, that works perfectly!