Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  126] [ 3]  / answers: 1 / hits: 15251  / 14 Years ago, mon, april 12, 2010, 12:00:00

page contents:



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


i want to get result:



1;2'3aaabbb


match tag is <b> and </b>



how to write this regex using javascript?
thanks!


More From » html

 Answers
14

Lazyanno,



If and only if:




  1. you have read SLaks's post (as well as the previous article he links to), and

  2. you fully understand the numerous and wondrous ways in which extracting information from HTML using regular expressions can break, and

  3. you are confident that none of the concerns apply in your case (e.g. you can guarantee that your input will never contain nested, mismatched etc. <b>/</b> tags or occurrences of <b> or </b> within <script>...</script> or comment <!-- .. --> tags, etc.)

  4. you absolutely and positively want to proceed with regular expression extraction



...then use:



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);


Produces:



1;2'3aaabbb

[#97099] Friday, April 9, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nolancampbellc

Total Points: 9
Total Questions: 102
Total Answers: 101

Location: Saint Vincent and the Grenadines
Member since Mon, Jan 16, 2023
1 Year ago
;