Regex: how to get content from inner tag (use javascript)?

page content:

aa<b>1;2'3</b>hh<b>aaa</b>..
 .<b>bbb</b>
blabla..

I want to get the result:

1;2'3aaabbb

match tag <b>and</b>

how to write this regex using javascript? thank!

+5
source share
5 answers

Lazyanno,

If and only if :

  • you read SLakspost (as well as the previous article that it refers to ), and
  • you fully understand the many and amazing ways in which extracting information from HTML using regular expressions can break and
  • , (, , , .. <b>/</b> <b> </b> <script>...</script> <!-- .. --> ..).

... :

var str = "aa<b>1;2'3</b>hh<b>aaa</b>..\n.<b>bbb</b>\nblabla..";

var match, result = "", regex = /<b>(.*?)<\/b>/ig;
while (match = regex.exec(str)) { result += match[1]; }

alert(result);

:

1;2'3aaabbb
+9

HTML .

Javascript DOM.

( jQuery):

var text = "";
$('<div>' + htmlSource + '</div>')
    .find('b')
    .each(function() { text += $(this).text(); });

HTML <div>, , <b>.

+8

jQuery:

// get all elements with a certain tag name
var b = document.getElementsByTagName("B");

// map() executes a function on each array member and
// builds a new array from the function results...
var text = b.map( function(element) {
  // ...in this case we are interested in the element text
  if (typeof element.textContent != "undefined")
    return element.textContent; // standards compliant browsers
  else
    return element.innerText;   // IE
});

// now that we have an array of strings, we can join it
var result = text.join('');
+2
      var regex = /(<([^>]+)>)/ig;
      var bdy="aa<b>1;2'3</b>hh<b>aaa</b>..\n.<b>bbb</b>\nblabla..";

      var result =bdy.replace(regex, "");
      alert(result) ;

: http://jsfiddle.net/abdennour/gJ64g/

+2

Just use '?' the character after the generation pattern for your inner text if you want to use regular expulsions. eg:

".*" to "(.*?)"
+1
source

All Articles