1

对不起这个糟糕的标题 - 不知道如何最好地表达它。

注意:我正在尝试在 Javascript 中执行此操作

我想匹配长度和结构不同的 2 个不同 url 中的特定文本。一个以 say 开头,video另一个以 say开头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"
4

3 回答 3

2

如果您使用的是 PHP 或 Perl,那么您可以使用\K锚点忽略正则表达式中匹配的所有内容,如下所示:

video.*\K%7bmatch%7

不幸的是,在 JavaScript 中,最简单的方法是使用以下正则表达式,它将匹配从 video until的所有内容%7bmatch%7d,然后您可以使用replace()它来摆脱不需要的数据:

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);

正则表达式 101 演示

于 2013-11-07T13:24:34.960 回答
1

看起来像那个网站上的一个错误。您应该使用/(?<=video.*)%7b.*?%7d/g,但该网站似乎认为它是向前看,而不是向后看(您可以通过将鼠标悬停在任一括号上来检查它)。


如果您需要匹配%7b.*?%7d来替换它,只需匹配所有内容并在替换中引用它,如下所示:

s/^(video.*?)%7b.*?%7d/$1your_replacement_here/g

请参阅regexr.com 上的演示和 regex101.com 上的另一个演示

于 2013-11-07T13:28:46.230 回答
0

这个正则表达式怎么样:

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];

希望这可以帮助,

问候,

马塞洛

于 2013-11-07T14:22:34.997 回答