0

I have a service that is returning a message. That message may be a combination of plain text or an html formatted text.

ex1: "This is a message"

ex2: "<p> This is also a message <p/>"

ex3: "This is also a <strong> message </strong>"

The thing we would like to do is come up with a script that would return as much plain text up until the first tag. So in the examples above:

  1. would return "This is a message.
  2. would return ""
  3. would return "This is also a"

I am not sure what approach is the best to do this. Can i accomplish this using Regex or JS. I know Regex can easily return text between two tags, but what i am looking for is a little different. Thanks in advance for any advice or help.

Gabriel
  • 211
  • 2
  • 4
  • 10

1 Answers1

3

The simplest solution would be to match anything except <s, starting at the beginning of the string:

match = subject.match(/^[^<]*/)[0];

This fails if <s could occur in comments/quoted strings before the first HTML tag, but that might not be a problem.

Test on JSFiddle

Explanation:

^      # Anchor the match to the start of the string.
[^<]   # Match any character that's not a <
*      # zero or more times (as many as possible).
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561