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.

1

1 Answer 1

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).
Sign up to request clarification or add additional context in comments.

7 Comments

Have you tested for last example.
Can you share a link where you tested it?
will it work for This is also a <strong> message </strong> string
In that example i would still want "This is also a"... so it would work for what i need it for.
@Braj: Of course it will, how is that so difficult to see or test?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.