简体   繁体   中英

How to extract href= and title values using javascript similar to PHP's preg_match_all()?

I got an HTML string as :var code; I want to extract all values of href and title similar to using PHP's preg_match_all() . I have done a similar thing in PHP as the example shown but wondering how I can do that in JavaScript and have all href and title values in array as PHP example ?

Note: initial value of $input is blocks of div like example below:

<div class="up"  rel="1234" id="up1234" style="float:right">
<div class="pic" style='border:3px solid #cacaca' >
 <a href="/episode 14" title="episode 14">
    <img src="images/image14.jpg" alt="watch episode 14"  />
 </a> 
</div>
<a href="/episode 14" title="episode 14 "class="title">watch episode 14</a> 
<div class="data" style="height:auto">watch episode 14 </div>
<center>
 <a href="episode14" title="episode 14" class="data" id="play_me1234">
 <img src="/images/image14.jpg" alt="watch episode 14"/>
 </a>
</center>
    </div>

preg_match_all('#href\s*=\s*"(.*)"#siU', $input, $foo1);
preg_match_all("#class=\"title\">([^<]+)</a>#", $input, $foo2);

$i = -1;
$foo1[1] = array_unique($foo1[1]);
 foreach($foo1[1] as $k => $v ){
  //echo $v;


 $i++;
echo '<a href="./doit.php?Id='.$v.'&title='.$foo2[1][$i].'"> '.$foo2[1][$i].' </a> <br />' ;
 }  
<script type="text/javascript">
    var pattern = /<a href="([^"]+?)" title="[^"]+?" class="title">([^<]+?)<\/a>/gi;
    code = code.match(pattern);
    for (i = 0; i < code.length; i++) {
        document.write(code[i].replace(pattern, '<a href="./doit.php?Id=$1&title=$2">$2</a><br />'));
    }
</script>

You want to use code.match(/your regex here/g) . The g modifier changes how match behaves so that it returns an array of full matches as opposed to just the first match, at the cost of losing the subpatterns. You can get them subpatterns back by matching the match against the regex again but this time without the modifier.

That said, you could just parse the DOM instead. If you have the HTML in an element, you can use querySelectorAll("[href]") to find elements with an href attribute and loop through them. Similarly, use querySelectorAll(".title") to find "title" elements more reliably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM