![](/img/trans.png)
[英]How to correctly rewrite only mysite.com to www.mysite.com and nothing else?
[英]How can I get the URL of an <a href=“http://www.mysite.com”> in JavaScript?
我正在用JavaScript處理一些文本(使用Node,所以請不要jQuery)。 我想提取頁面上<a>
標記中的所有URL。 如何在JavaScript中執行此操作?
var myArray = preg_match_all("<a.*?href=[\'|\"](.*?)[\'|\"]", "How can I get the URL of an <a href=\"http://www.mysite.com\"> in JavaScript? How can I get the URL of an <a href=\"http://www.mysite.org\"> in JavaScript?")
if ( myArray != null) {
for ( i = 0; i < myArray.length; i++ ) {
alert(myArray[i]);
}
}
function preg_match_all(regex, haystack) {
var globalRegex = new RegExp(regex, 'g');
var globalMatch = haystack.match(globalRegex);
matchArray = new Array();
for (var i in globalMatch) {
nonGlobalRegex = new RegExp(regex);
nonGlobalMatch = globalMatch[i].match(nonGlobalRegex);
matchArray.push(nonGlobalMatch[1]);
}
return matchArray;
}
preg_match_all函數取自http://coding.pressbin.com/16/Javascript-equivalent-of-PHPs-pregmatchall
我建議使用這樣的在線測試儀: http : //www.pagecolumn.com/tool/regtest.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.