Regex - match string with special characters between delimiter (html – Aaron May 09 '19 at 12:26

  • The text is a value for an [innerHTLM] directive later on. My problem is that I'm querying an catch all emailbox and if the email contains HTML I want to remove the included styling first. tldr: it gets parsed later on anyway. ([^<]*) didn't find a match at all anymore. – hi im vinzent May 09 '19 at 12:32
  • @MattEllen While the title of that proposed duplicate looks relevant, the accepted answer there certainly wouldn't help OP – P Varga May 09 '19 at 12:45
  • 1
    @ᆼᆺᆼ true, but the most upvoted answer does help. – Matt Ellen May 09 '19 at 12:46
  • Most likely it is a duplicate or similar to a past question but not to one of the mentioned questions, as the problem is not the multiple lines but matching a string over "span line boundaries". – hi im vinzent May 09 '19 at 13:28
  • 1 Answers1

    3

    I think you are looking for the s regex flag which allows matches to span line boundaries:

    Something like (?s)<style[^>]*>(.*?)<\/style>, try it here.
    (/<style[^>]*>(.*?)<\/style>/gs in JS syntax)

    P Varga
    • 19,174
    • 12
    • 70
    • 108
    • 1
      Exactly what I was looking for, thank you! I edited the answer and added the working JavaScript sequence which hopefully helps others with the same problem later on. – hi im vinzent May 09 '19 at 13:17
    • I've implemented the solution mentioned above. It worked like a charm in Chrome but after testing it with different browsers I figured out FF is not able to handle the /s regex flag. Any experience with this issue? "SyntaxError: invalid regular expression flag s" – hi im vinzent May 09 '19 at 14:47
    • 1
      /s regex flag was implemented with ES9/ES2018 and is for now only implemented in Chrome: https://flaviocopes.com/es2018/ – hi im vinzent May 09 '19 at 15:00
    • @hiimvinzent Are you transpiling your JS? It is likely that `/s` has a corresponding transform, for example here is the one for Babel: https://babeljs.io/docs/en/babel-plugin-transform-dotall-regex – P Varga May 09 '19 at 19:43
    • Moved the stripping with regex to a backend layer for now. Looks like a proper approach but I didn't try it yet. – hi im vinzent May 10 '19 at 11:10