对不起这个糟糕的标题 - 不知道如何最好地表达它。
注意:我正在尝试在 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"