很抱歉这个糟糕的标题 - 不确定如何最好地把它。
请注意: 我试图在Javascript中执行此操作
我希望匹配2个不同网址中的特定文本,这些网址具有不同的长度和结构。一个以video
开头,另一个以audio
开头。必须根据字符串的开头不同地替换匹配。
我无法弄清楚如何在积极的外观中进行dotall(。*)匹配?
例如,下面的字符串应该由2个单独的RegEx匹配,一个指定video
而另一个audio
必须在%7bmatch%7d
之前
对于给定的字符串开头,它们必须仅匹配%7bmatch%7d
。
video://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length
audio://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length
我试过了:
/(?<=video)(?<=.*)%7bmatch%7d/g
http://regexr.com?373gh
除了其他变体之外,似乎只能匹配%7bmatch%7d
或任何其他匹配,而不会获得示例中video
之后的所有内容。
让我难过,真的很感激在正确的方向上轻推。
编辑:特定于Javascript的答案:
// Random collection of string(s)
var inputString = "video://lots0fr@ndomcharacasdf/asd/ft#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharact#Rsinbet/asdfas/dweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinbetweasdf/asd/f/asdfenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfafsdct#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinfasdfasdfbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfasdf///asdfasdfct#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length";
// Regex to match after every `video` occurence
regexVideo = new RegExp(/(video.*?)%7bmatch%7d/g);
// Regex to match after every `audio` occurence
regexAudio = new RegExp(/(audio.*?)%7bmatch%7d/g);
// strings to replace the matches
var replaceStringVideo = "<span class='woot'>W00tW00tVideoW00t</span>";
var replaceStringAudio = "<span class='woot'>W00tW00tVAudioW00t</span>";
// replace both audio and video with respective replace values NOTE: the dollar token needed concatenated before the replaceString.
inputString.replace(regexVideo, "$1" + replaceStringVideo).replace(regexAudio, "$1" + replaceStringAudio);
// This yields an inputString value of
"video://lots0fr@ndomcharacasdf/asd/ft#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharact#Rsinbet/asdfas/dweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinbetweasdf/asd/f/asdfenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfafsdct#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinfasdfasdfbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfasdf///asdfasdfct#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length"
答案 0 :(得分:2)
如果您使用的是PHP或Perl,那么您可以使用\K
锚点忽略正则表达式中之前匹配的所有内容,如下所示:
video.*\K%7bmatch%7
在JavaScript中遗憾的是,最简单的方法是使用以下正则表达式,该表达式匹配从视频到%7bmatch%7d
的所有内容,然后您可以使用replace()
来删除不需要的数据:< / p>
var input = "video://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length";
var finalResult = /(?=video.*%7bmatch%7d).*%7bmatch%7d/.exec(input)[0].replace(/.*(?=%7bmatch%7d)/, "");
console.log(finalResult);
答案 1 :(得分:1)
看起来像该网站上的错误。你应该使用/(?<=video.*)%7b.*?%7d/g
,但该网站似乎认为它是一个先行,而不是一个后视(你可以通过将鼠标悬停在两个括号上来检查它。)
如果您需要匹配%7b.*?%7d
来替换它,只需将所有内容与之匹配并参考替换中的内容,如下所示:
s/^(video.*?)%7b.*?%7d/$1your_replacement_here/g
答案 2 :(得分:0)
这个RegExp怎么样:
var regExpVideo = /(?:video):\/\/[^%]+([^\/]+)/
var regExpAudio = /(?:audio):\/\/[^%]+([^\/]+)/
var videoSource = regExpVideo.exec("video://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length")[1];
var audioSource = regExpAudio.exec("audio://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length")[1];
希望这有帮助,
此致
马塞洛