【问题标题】:Match a string in a string containing certain string in Javascript匹配包含 Javascript 中特定字符串的字符串中的字符串
【发布时间】:2013-11-07 13:15:43
【问题描述】:

抱歉标题太糟糕了 - 不知道怎么写才好。

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

我想匹配 2 个不同长度和结构的 url 中的特定文本。一个以video 开头,另一个以audio 开头。必须根据字符串的开头以不同的方式替换匹配项。

我不知道如何在积极的后视中进行 dotall (.*) 匹配?

例如,下面的字符串应该由两个独立的正则表达式匹配,一个指定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/ghttp://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"

【问题讨论】:

  • 您使用什么语言或工具?
  • 如果我可以在 regexr.com 上使用 javascript,我将使用它
  • 它不适用于正则表达式,因为它似乎不支持可变长度的lookbehinds。如果您将 .* 更改为 .{x} ,其中 x 是匹配的确切字符数。
  • 为什么只希望%7bmatch%7d匹配?
  • @SQB 因为我希望能够替换它。

标签: javascript regex regex-lookarounds regex-greedy


【解决方案1】:

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

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

【讨论】:

  • @RobSchmuecker 不客气,但请注意 JavaScript 不支持 \K 锚点,也不支持查看后面。
  • 啊,我刚刚注意到 - 关于让它在 JS 中工作的任何提示?
  • @RobSchmuecker 真的很抱歉,但我必须马上离开。如果我很快回来,我会尝试在 JavaScript 中发布一个可行的解决方案。再次非常抱歉。
  • @RobSchmuecker 我已经编辑了我的答案以包含 JavaScript 的解决方案。
  • 非常感谢您的所有努力 - 这就是为什么我给@SQB jsfiddle.net/robschmuecker/zHQAZ/embedded/result 提供最佳答案的原因感谢您的所有帮助,非常感谢。
【解决方案2】:

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


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

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

demo on regexr.com 和另一个demo on regex101.com

【讨论】:

  • JS 不支持lookbehinds。而且大多数引擎甚至不支持您建议的无限后视。
  • @SQB 该网站确实有它的工具提示混乱我同意但是前瞻/后视示例按预期工作,只是工具提示是错误的。
  • 是啊,刚才才看到asker想用JavaScript。并且示例中有lookbehind,所以我假设它是一种支持lookbehinds的方言。
  • @SQB - 非常感谢,这似乎通过对 JS 的一些重构完成了技巧;)+1 jsfiddle.net/robschmuecker/zHQAZ/embedded/result
【解决方案3】:

这个正则表达式怎么样:

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

希望这会有所帮助,

问候,

马塞洛

【讨论】:

  • 您好 Mindastic,非常感谢您的回答,这就是我接受 @SQB 的回答 jsfiddle.net/robschmuecker/zHQAZ/embedded/result 的原因
  • 嗨,罗伯。我刚刚更新了我的答案,因为当我写它时,我想使用 exec 而不是 match。我不知道为什么我使用匹配,因为匹配是字符串而不是正则表达式。无论如何,我很高兴看到你得到了一个有用的答案:)。 Rgds。
猜你喜欢
  • 2010-11-22
  • 2019-06-05
  • 1970-01-01
  • 2020-12-25
  • 2019-04-24
  • 1970-01-01
  • 2011-10-02
  • 2017-05-26
  • 1970-01-01
相关资源
最近更新 更多