PHP Preg_match_all in Javascript
I've got this php code that works perfectly:
preg_match_all(
'/<div style="display:block; float:left; height:240px; width:150px;'
. ' text-align:center; margin:5px; overflow:hidden;">.开发者_如何学C*?'
. '<span class="card-text">(.*?)'
. '<span class="instruction">\((.*?)\)<\/span>/s',
$html,
$students,
PREG_SET_ORDER
);
foreach($students as $student) {
echo($student[1].",".$student[2]."<br />");
}
How would I do this in javascript? Thanks!
Frankly you should not do this using JavaScript. You should not be trying to parse so much HTML using regular expressions either.
Instead you should consider looking at something like Simple HTML Dom parser
As for a JavaScript solution - JS is a DOM parser!
var student1 = document.getElementsByClassName('card-text')[0];
var student2 = document.getElementsByClassName('instruction')[0];
//echo out
A better solution would be to use jQuery, since you'll have fewer problems with DOM manipulation (its selector engine is fantastic!) see rough example here.
精彩评论