正则:在第一个后斜线发生后寻找一个特定的单词
我想在第一个“/”出现之后使用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;
}
}
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用此正则罚款:
regex demo
regex详细信息:
^
:开始[^\/]+
:匹配不是/
的任何字符的1个或更多字符\/
:匹配/
[^\/]*
:匹配不是/<的任何字符的0或更多字符/code>
bacon
。
\ bbacon \ b
:匹配完整的 >You may use this regex for this:
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 wordbacon
.*
: Match remaining text on this lineCode:
尝试使用以下正则态度:
如果您获得匹配项,则意味着您的URL中有这个单词。如果您需要匹配案例,则可以使用“ I”修饰符。
REGEX说明:
\/
:slash。*
:任何字符\ bbacon \ bbacon \ b
:Word Borgaries之间的热门单词检查DEMO 在这里。
编辑:如果您想匹配培根“ word ”,特别是在第一次发生后,请检查Anubhava的答案。
Try using the following regex:
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 boundariesCheck the demo here.
EDIT: If you want to match your bacon "word" specifically after the first occurrence, check anubhava's answer.
你尝试过吗?
Have you tried this?
我会选择一个简单的一个:
const regex =/\/.*(bacon)/
\/
匹配/
。*
0 to n artaration(培根)
您的模式(括号是可选的)请注意,如果您的链接为
http:// baconurl/…
I would go with a simple one :
const regex = /\/.*(bacon)/
\/
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/…