简体   繁体   English

正则表达式:在第一个反斜杠出现后查找特定单词

[英]Regex: Look for a specific word after the first backslash occurrence

I'm wanting to use regex to look for the word "bacon" after the first "/" occurrence.我想在第一次出现“/”之后使用正则表达式来查找“bacon”这个词。

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,})$");
  if (regex.test(url)) {
      return true;
  } else {
      return false;
  }
}

Thanks!谢谢!

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 / [^\/]+ :匹配 1 个或多个不是/的任何字符
  • \/ : Match a / \/ : 匹配一个/
  • [^\/]* : Match 0 or more of any character that is not a / [^\/]* : 匹配 0 个或多个不是/的任何字符
  • \bbacon\b : Match complete word bacon \bbacon\b : 匹配完整的单词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/'));

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.如果您需要不区分大小写,则可以使用“i”修饰符。

Regex Explanation:正则表达式解释:

  • \/ : a slash \/ : 斜线
  • .* : any character .* : 任何字符
  • \bbacon\b : your hotword between word boundaries \bbacon\b :您在单词边界之间的启动指令

Check the demo here .此处查看演示。


EDIT : If you want to match your bacon " word " specifically after the first occurrence, check anubhava's answer.编辑:如果您想在第一次出现之后专门匹配您的培根“单词”,请检查 anubhava 的答案。

I would go with a simple one : const regex = /\/.*(bacon)/我会选择一个简单的: 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 .* 0 到 n 个字符
  • (bacon) your pattern (parenthesis are optional) (bacon)你的模式(括号是可选的)

Be aware it will not work if your link is http://baconurl/…请注意,如果您的链接是http://baconurl/…

你试过这个吗?

/\/.*bacon/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM