gpt4 book ai didi

javascript - 匹配包含Javascript中特定字符串的字符串中的字符串

转载 作者:行者123 更新时间:2023-11-30 12:54:12 25 4
gpt4 key购买 nike

抱歉这个糟糕的标题 - 不知道怎么说才好。

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

我想匹配 2 个不同长度和结构的不同 URL 中的特定文本。一个以 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"

最佳答案

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

video.*\K%7bmatch%7

不幸的是,在 JavaScript 中,最简单的方法是使用以下正则表达式来匹配从视频到 %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);

Regex101 Demo

关于javascript - 匹配包含Javascript中特定字符串的字符串中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836953/

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