正则:在第一个后斜线发生后寻找一个特定的单词

发布于 2025-02-13 07:44:41 字数 887 浏览 0 评论 0 原文

我想在第一个“/”出现之后使用Regex查找“培根”一词。

例如:

应该返回true:

console.log('1 - ', myRegexFunction('www.bacondelivery.com/weekly-bacon-delivery/'));
console.log('2 - ', myRegexFunction('www.bacondelivery.com/daily-bacon-delivery/'));
console.log('3 - ', myRegexFunction('www.bacondelivery.com/bacon-of-the-month-club/'));

应该返回false:

console.log('4 - ', myRegexFunction('www.bacondelivery.com/'));
console.log('5 - ', myRegexFunction('www.bacondelivery.com/?some_param'));
console.log('6 - ', myRegexFunction('www.bacondelivery.com/about/'));
console.log('7 - ', myRegexFunction('www.bacondelivery.com/contact-us/'));

这是我目前拥有的:

function myRegexFunction(url) {
  var regex = new RegExp("^([a-z0-9]{5,})$");
  if (regex.test(url)) {
      return true;
  } else {
      return false;
  }
}

谢谢!

I'm wanting to use regex to look for the word "bacon" after the first "/" occurrence.

For example:

Should return true:

console.log('1 - ', myRegexFunction('www.bacondelivery.com/weekly-bacon-delivery/'));
console.log('2 - ', myRegexFunction('www.bacondelivery.com/daily-bacon-delivery/'));
console.log('3 - ', myRegexFunction('www.bacondelivery.com/bacon-of-the-month-club/'));

Should return false:

console.log('4 - ', myRegexFunction('www.bacondelivery.com/'));
console.log('5 - ', myRegexFunction('www.bacondelivery.com/?some_param'));
console.log('6 - ', myRegexFunction('www.bacondelivery.com/about/'));
console.log('7 - ', myRegexFunction('www.bacondelivery.com/contact-us/'));

Here's what I currently have:

function myRegexFunction(url) {
  var regex = new RegExp("^([a-z0-9]{5,})
quot;);
  if (regex.test(url)) {
      return true;
  } else {
      return false;
  }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弱骨蛰伏 2025-02-20 07:44:41

您可以使用此正则罚款:

^[^\/]+\/[^\/]*\bbacon\b.*

regex demo

regex详细信息:

  • ^:开始
  • [^\/]+:匹配不是/的任何字符的1个或更多字符
  • \/:匹配/
  • [^\/]*:匹配不是/<的任何字符的0或更多字符/code>
  • 单词 bacon

\ bbacon \ b :匹配完整的 >

function myRegexFunction(url) {
  const regex = /^[^\/]+\/[^\/]*\bbacon\b.*/;
  return regex.test(url);
}

console.log('1 - ', myRegexFunction('www.bacondelivery.com/weekly-bacon-delivery/'));

console.log('2 - ', myRegexFunction('www.bacondelivery.com/daily-bacon-delivery/'));
console.log('3 - ', myRegexFunction('www.bacondelivery.com/bacon-of-the-month-club/'));

console.log('4 - ', myRegexFunction('www.bacondelivery.com/'));
console.log('5 - ', myRegexFunction('www.bacondelivery.com/?some_param'));
console.log('6 - ', myRegexFunction('www.bacondelivery.com/about/'));
console.log('7 - ', myRegexFunction('www.bacondelivery.com/contact-us/'));

You may use this regex for this:

^[^\/]+\/[^\/]*\bbacon\b.*

RegEx Demo

RegEx Details:

  • ^: Start
  • [^\/]+: Match 1 or more of any character that is not a /
  • \/: Match a /
  • [^\/]*: Match 0 or more of any character that is not a /
  • \bbacon\b: Match complete word bacon
  • .*: Match remaining text on this line

Code:

function myRegexFunction(url) {
  const regex = /^[^\/]+\/[^\/]*\bbacon\b.*/;
  return regex.test(url);
}

console.log('1 - ', myRegexFunction('www.bacondelivery.com/weekly-bacon-delivery/'));

console.log('2 - ', myRegexFunction('www.bacondelivery.com/daily-bacon-delivery/'));
console.log('3 - ', myRegexFunction('www.bacondelivery.com/bacon-of-the-month-club/'));

console.log('4 - ', myRegexFunction('www.bacondelivery.com/'));
console.log('5 - ', myRegexFunction('www.bacondelivery.com/?some_param'));
console.log('6 - ', myRegexFunction('www.bacondelivery.com/about/'));
console.log('7 - ', myRegexFunction('www.bacondelivery.com/contact-us/'));

逐鹿 2025-02-20 07:44:41

尝试使用以下正则态度:

\/.*\bbacon\b

如果您获得匹配项,则意味着您的URL中有这个单词。如果您需要匹配案例,则可以使用“ I”修饰符。

REGEX说明:

  • \/:slash
  • 。*:任何字符
  • \ bbacon \ bbacon \ b :Word Borgaries之间的热门单词

检查DEMO 在这里


编辑:如果您想匹配培根“ word ”,特别是在第一次发生后,请检查Anubhava的答案。

Try using the following regex:

\/.*\bbacon\b

If you get a match, it means you have that word in your url. If you need to match it case-insentitively, then you can use the 'i' modifier.

Regex Explanation:

  • \/: a slash
  • .*: any character
  • \bbacon\b: your hotword between word boundaries

Check the demo here.


EDIT: If you want to match your bacon "word" specifically after the first occurrence, check anubhava's answer.

埋葬我深情 2025-02-20 07:44:41

你尝试过吗?

/\/.*bacon/

Have you tried this?

/\/.*bacon/
只怪假的太真实 2025-02-20 07:44:41

我会选择一个简单的一个: const regex =/\/.*(bacon)/

function myRegexFunction(url) {
  var regex = /\/.*(bacon)/;
  if (regex.test(url)) {
      return true;
  } else {
      return false;
  }
}
  • \/匹配/
  • 。* 0 to n artaration
  • (培根)您的模式(括号是可选的)

请注意,如果您的链接为 http:// baconurl/…

I would go with a simple one : const regex = /\/.*(bacon)/

function myRegexFunction(url) {
  var regex = /\/.*(bacon)/;
  if (regex.test(url)) {
      return true;
  } else {
      return false;
  }
}
  • \/ to match after a /
  • .* 0 to n character
  • (bacon) your pattern (parenthesis are optional)

Be aware it will not work if your link is http://baconurl/…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文