Javascript get last html tag in textarea

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Been trying to figure this out for a while now guys, and need your help. I'm trying to find the last opened html element in a textarea. I'm running into trouble in several areas. It's not very efficient to run the search on the entire string, instead I need to run from the end of the string and stop once the tag is found. I need to be returned the html element, stripped of attributes and brackets. It also needs to ignore self closing tags. Here's an example of what I mean:
HTML Code: [ Select ]
<h1>Hello</h1>
<p>Augue pid rhoncus. Proin hac placerat, lectus diam, <a href="#"><img src="#" /></a></p>
 
  1. <h1>Hello</h1>
  2. <p>Augue pid rhoncus. Proin hac placerat, lectus diam, <a href="#"><img src="#" /></a></p>
  3.  

I would need the javascript function to return "a". Any ideas? Obviously going to be looking at string manipulation and possibly regular expressions, but so far my attempts have been futile.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

This may help point you in the right direction.

JAVASCRIPT Code: [ Select ]
var html = '<h1>Hello</h1><p>Augue pid rhoncus. Proin hac placerat, lectus diam, <a href="#" class="ds"><img src="#" /></a></p>'
var matches = html.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi,html)
console.log(matches);
 
for ( x in matches )
{
    matches[x] = matches[x].replace(/\s[^\/>]+/g,'');
}
console.log(matches);
  1. var html = '<h1>Hello</h1><p>Augue pid rhoncus. Proin hac placerat, lectus diam, <a href="#" class="ds"><img src="#" /></a></p>'
  2. var matches = html.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi,html)
  3. console.log(matches);
  4.  
  5. for ( x in matches )
  6. {
  7.     matches[x] = matches[x].replace(/\s[^\/>]+/g,'');
  8. }
  9. console.log(matches);


Code: [ Select ]
First Matches: [ '<h1>',
 '</h1>',
 '<p>',
 '<a href="#" class="ds">',
 '<img src="#" />',
 '</a>',
 '</p>' ]
After Replace Matches: [ '<h1>', '</h1>', '<p>', '<a>', '<img/>', '</a>', '</p>' ]
[Finished]
  1. First Matches: [ '<h1>',
  2.  '</h1>',
  3.  '<p>',
  4.  '<a href="#" class="ds">',
  5.  '<img src="#" />',
  6.  '</a>',
  7.  '</p>' ]
  8. After Replace Matches: [ '<h1>', '</h1>', '<p>', '<a>', '<img/>', '</a>', '</p>' ]
  9. [Finished]

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.