如何通过没有模式字符串的正则表达式获得所有干净的链接[重复]
Posted
技术标签:
【中文标题】如何通过没有模式字符串的正则表达式获得所有干净的链接[重复]【英文标题】:How can i get all clean links via Regex without pattern string [duplicate] 【发布时间】:2018-11-23 00:20:19 【问题描述】:当我尝试这场比赛时,我会得到这样的链接;
结果:
"video","src":"https:\/\/video-mxp1-1.xx.fbcdn.net\/v\/t42.9040-2\/34384597_178997956146914_227512178675023872_n.mp4"
我想得到这样的清理链接:https:\/\/video-mxp1-1.xx.fbcdn.net\/v\/t42.9040-2\/34384597_178997956146914_227512178675023872_n.mp4
感谢您的帮助; 我的代码如下:
var VideoLinks='"type":"video","src":"https:\\\/\\\/video-mxp1-1.xx.fbcdn.net\\\/v\\\/t42.9040-2\\\/34333324_n.mp4?_nc_cat=0&efg=ey1&oe=5B211DB0","width":null"type":"video","src":"https:\\\/\\\/video-mxp1-1.xx.fbcdn.net\\\/v\\\/t42.9040-2\\\/34333324_n.mp4?_nc_cat=0&efg=ey1&oe=5B211DB0","width":null';
My Pattern
pattern =/"video","src":"(.*?)"/g;
var videos=Sitestring.match(pattern);
console.log(videos);
【问题讨论】:
你遇到了什么错误? 没有错误,我通过“video”,“src”:“文本获取链接,但我不想要这个文本,我想要清理链接,例如;https:\\\/\\\/video -mxp1-1.xx.fbcdn.net\\\/v\\\/t42.9040-2\\\/34333324_1111077645701352_5020419359995068416_n.mp4 not:"video","src":"https:\\\/\\ \/video-mxp1-1.xx.fbcdn.net\\\/v\\\/t42.9040-2\\\/34333324_1111077645701352_5020419359995068416_n.mp4 【参考方案1】:const str = '"video","src":"https:\vid1.mp4"';
const regex = new RegExp('src\":\"(.*)\"', 'i');
console.log(regex.exec(str)[1]);
关于多次出现
function getMatch(str, regex)
const matches = [];
let oneMatch;
do
oneMatch = regex.exec(str);
if (!oneMatch)
return matches;
matches.push(oneMatch[1]);
while (oneMatch);
const str = '"video","src":"https:vid1.mp4","video","src":"https:vid2.mp4","video","src":"https:vid3.mp4"';
const regex = new RegExp('src\":\"(.*?)\"', 'ig');
console.log(getMatch(str, regex));
【讨论】:
谢谢你的回答,但我想在 1 场比赛中解决这个问题,这可能吗? var VideoLinks='"type":"video","src":"https:\\\/\\\/video-mxp1-1.xx.fbcdn.net\\\/v\\\/t42.9040 -2\\\/34333324_n.mp4?_nc_cat=0&efg=ey1&oe=5B211DB0","width":null"type":"video","src":"https:\\\/\\\/video-mxp1 -1.xx.fbcdn.net\\\/v\\\/t42.9040-2\\\/34333324_n.mp4?_nc_cat=0&efg=ey1&oe=5B211DB0","width":null'; @MahmutDuman 我用多个视频结果编辑了我的帖子 :) 哇,谢谢它真棒,为我工作:)以上是关于如何通过没有模式字符串的正则表达式获得所有干净的链接[重复]的主要内容,如果未能解决你的问题,请参考以下文章