Javascript get last html tag in textarea

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post August 3rd, 2011, 5:32 pm

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.
Use your words like arrows to shoot toward your goal.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 3rd, 2011, 5:32 pm

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post August 3rd, 2011, 11:09 pm

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]
#define NULL (::rand() % 2)

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 183 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.