gpt4 book ai didi

javascript - 正则表达式不匹配内容,而是匹配整个搜索字符串

转载 作者:行者123 更新时间:2023-12-02 14:13:09 24 4
gpt4 key购买 nike

我正在使用此正则表达式来匹配 <a> 中的“href”属性标签:

var href_matches = postRep.match(/href="(.*?)"/g);

正则表达式正确匹配 href,但它返回整个“href=http:example.com”字符串。
如何设法仅获取 href 值(例如“example.com”)?

最佳答案

您可以在正则表达式上运行 exec() :

var url_match = /href="(.*?)"/g.exec(postRep);

或删除全局标志

var url_match = postRep.match(/href="(.*?)"/);

Using String's match() function won't return captured groups if the global modifier is set.

关于javascript - 正则表达式不匹配内容,而是匹配整个搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com