0

The question might be more confusing than what I am trying to do. Basically I want to get the content of the html <title element:

s = s.match(/<title>(.*?)<\/title>/);

This gives me:

["<title>foobar</title>", "foobar"]

So to access the text only, I have to use s[1].

Is there any way to create a match with just the text, skipping "<title>foobar</title>"? Or with other words: is there a way to tell match(): "search for a string delimited with <a></a> but ignore those delimiters in the result"?

I have tried various expressions with negative lookbehinds and such, but I wasnt lucky. I dont even know if that is the correct approach.

Alex
  • 9,339
  • 2
  • 28
  • 46
  • 1
    There's been load of such questions. Read documentation. JS does not support lookbehinds, only lookaheads. Use `RegExp.exec` and get the captured texts. – Wiktor Stribiżew Jan 18 '16 at 11:08
  • @WiktorStribiżew thanks, I've looked on SO already. Do you have a similar question? – Alex Jan 18 '16 at 11:10
  • If the `s[1]` part really bothers you that much, you can do `s = s.match(/(.*?)/)[1];` (but only if you're sure that there will always be a match.) – JJJ Jan 18 '16 at 11:12
  • 3
    Actually, I can also add: *parsing HTML with regex is not recommended*. :) – Wiktor Stribiżew Jan 18 '16 at 11:12
  • @WiktorStribiżew I know that its evil, but I have no choice :( – Alex Jan 18 '16 at 11:13
  • @WiktorStribiżew so the answer is, its not possible to exclude those matches from the result? – Alex Jan 18 '16 at 11:15
  • @Alex: Right, I already mentioned that in my first comment in a *JS does not support lookbehinds*. – Wiktor Stribiżew Jan 18 '16 at 11:15
  • @WiktorStribiżew but not clearly :) thanks anyways – Alex Jan 18 '16 at 11:15
  • Please clarify why you *"have no choice"* but to use a regex to parse html? Is it a business decision (your client has, for some reason, specified that you must use a specific technology to solve their business problem)? "I have no choice" sounds fishy. Many (most?) issues are that are considered "no choice" are actually an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). For example you could simply parse the html into jquery and use `$("title").text()` – freedomn-m Jan 18 '16 at 11:30
  • @freedomn-m i would love to, but the comment characters limit is too low to fully explain. I am fully aware of how evil html parsing with regex is and for the most part I dont even do, just for some `` related parts. To give some hints why I have to some parsing: ajax, history.js, Drupal – Alex Jan 18 '16 at 11:45
  • None of which explain why you can't parse what you want rather than use a regex. If there's a good reason, then fine, but at least 10% of questions on SO are XY problems because someone (in the chain) came up with a solution to a problem and then tried to get answers to problems with that solution rather than answers to the *actual* problem. – freedomn-m Jan 18 '16 at 13:49
  • @freedomn-m thats true, but I dont apply to that XY problem in this case :) thanks a lot for your input! – Alex Jan 18 '16 at 13:53
  • @freedomn-m http://stackoverflow.com/questions/35149397/should-i-use-an-iframe-rather-than-using-ajax maybe you have a hint or two – Alex Feb 02 '16 at 11:21

0 Answers0