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>
<b>
</b>
how to write this regex using javascript? thank!
Lazyanno,
Lazyanno
If and only if :
SLaks
<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);
:
HTML .
Javascript DOM.
( jQuery):
var text = ""; $('<div>' + htmlSource + '</div>') .find('b') .each(function() { text += $(this).text(); });
HTML <div>, , <b>.
<div>
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('');
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/
Just use '?' the character after the generation pattern for your inner text if you want to use regular expulsions. eg:
".*" to "(.*?)"