简体   繁体   中英

javascript: extract parts of a string (regex)

The question is simple, assume the following string:

var str = '<a href="aaaa">aaaaa</a><a href="bb">b\'s</a>'

How do you extract the value of href. I would think something like

var arr = str.match(/(?:href=")(\w+)/g) ;
--> ["href="aaaa", "href="bb"]

Of course I want

["aaaa", "bb"]

Withoug the /g it get close, but it only matches "aaaa". Any suggestions how to fix this ?

Thanks!

Because Javascript doesn't have lookbehind, this may be what you want. Naturally there will be more elegant solutions:

input.match(/<[^href|/]*(href[\s]*=[\s]*")([^"]+)(?=">)/g).map(
function(x){return x.split('href')[1].replace(/[^"]+"(.*)/,'$1');
})

Additionally, you may be better off getting a HTML parsing plugin. And extracting the properties you need using that.

Cheers.

DOM parsing with JS is so easy.

var str = '<a href="aaaa">aaaaa</a><a href="bb">b\'s</a>',
    help = document.createElement('div');

helper.innerHTML = str;

Array.prototype.forEach.call(help.querySelectorAll("a[href]"), function (elem) {
    console.log(elem.getAttribute('href'));
});

http://jsfiddle.net/ExplosionPIlls/gtdFh/

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