0

I am trying to get the text available inside the <a> elements. I have used the innerHTML to retrieve that inner text, but in few <a> elements the inner Text is placed inside the <span> elements.

While retrieving the text value am getting it as <span>sample text</span> as the output string. Can anyone help me to remove the <span></span> in the output string using RegEx so that i can only have sample text in my output string.

<a href="#"><span>Sample Text</span></a>

Note: I am not using any Javascript Libraries in the page.

codeMad
  • 15
  • 6
  • 3
    [HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain, the song of re̸gular exp​ression parsing will exti​nguish the voices of mor​tal man from the final ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the omes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – adeneo Sep 18 '13 at 07:30
  • Leaving the obligatory link for [why you shouldn't parse HTML with Regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – CodingIntrigue Sep 18 '13 at 07:31

3 Answers3

2

innerText or textContent (depending on the browser) gets the elements text without the span tags :

var elem = document.getElementsByTagName('a')[0],
    text = ('innerText' in elem)? 'innerText' : 'textContent';

var content = elem[text];

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

If you can include jQuery, you can use - $("a").text() without using any regex.

And with pure JavScript -

/* elem you can get in many ways, by id, by tagname or by class etc.
   I am using tagName for your specific case
*/
var elem = document.getElementsByTagName('a')[0],

/* use innerText or textContent, whichever is available in browser */     
var content = elem.innerText || elem.textContent;
Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35
0

You can use querySelectorAll to find all <a> tags with <span> children and take the text as necessary:

var elems = document.querySelectorAll("a > span");
for(var e in elems) {
  var element = elems[e];
  var aTag = element.parentNode;
  if(aTag.innerText) aTag.innerText = element.innerText;
  else aTag.textContent = element.textContent;
}

Here is a jsFiddle to demonstrate the above.

To clarify, document.querySelectorAll is only natively supported on IE8+ (and most other modern browsers) but polyfills are available.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176