2

How do I match the end of a regular expression by a word? For example:

<h1><a href=...></a>CONTENT</h1>

Given that <h1> is the start tag, how do I return <a href=...></a>CONTENT?

The expression /< h1>(([<\/h1>\b])*/ does not seem to work

5
  • 3
    stackoverflow.com/a/1732454/383402 Commented Feb 4, 2012 at 0:25
  • @Borealid: Without even clicking it, I knew which post that was :) Commented Feb 4, 2012 at 0:30
  • 3
    @Borealid: Give it a rest. There's no nested tags here, nor did the question ask for parsing. Also it's kind of rude to spam it onto every newbie question. meta.stackexchange.com/a/73168/148103 Commented Feb 4, 2012 at 0:33
  • 1
    @mario The two web sites mentioned in that response are actually useful, thanks! I'll use them instead. As for applicability: in my experience, one tag becomes two, and then someone puts in an HTML entity, and then you end up with a comment, and before you know it, you're parsing HTML. Commented Feb 4, 2012 at 0:40
  • @Borealid: Right, but that's true of everything. You start out with a simple for loop, and then requirements change, and new requirements are added, and before you know it you have a 2000-line function. Because everyone knows that you can never, ever change your design later. Refactoring is simply out of the question! Right? Commented Feb 4, 2012 at 3:37

2 Answers 2

3
/<h1>([\s\S]*)<\/h1>/

I think this should help you out.

Sign up to request clarification or add additional context in comments.

1 Comment

Well then this should do it /<h1>([\x20-\x7E]*)<\/h1>/g
0

What makes most regular expressions difficult, is the fact that they're greedy by default. By making them ungreedy using the U modifier, writing something like this becomes very trivial. The following should work.

/<h1>(.*)<\/h1>/U

1 Comment

@M42 Yes it does, because I made it ungreedy. Please read my answer and the link about the U modifier. Or even better, test it yourself.

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.