Javascript 正则表达式:在另一个字符串的中间匹配URL字符串

Javascript 正则表达式:在另一个字符串的中间匹配URL字符串,javascript,regex,url,Javascript,Regex,Url,正在学习regexp,但目前我对这种模式有点迟钝 我需要在url字符串中匹配我的字符串,例如: 如果url包含字符串'/example/regex',则应返回true /REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new') // => true /REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new/boo') // =>

正在学习regexp,但目前我对这种模式有点迟钝

我需要在url字符串中匹配我的字符串,例如:

如果url包含字符串
'/example/regex'
,则应返回true

/REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new') // => true
/REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new/boo') // => false

谢谢

您只需避开反斜杠,需要稍微清理一下

 /\/example\/regex/.test('http://test.com/example/regex/new/boo')

根据您作为示例给出的内容,两者都应该进行验证。

在您的示例中,使用$you可以表示正则表达式的结尾

/\/example\/regex\/\w*$/.test('http:\/\/test.com\/example\/regex\/new') => true
/\/example\/regex\/\w*$/.test('http:\/\/test.com\/example\/regex\/new\/boo') => false

如果您不想再使用斜杠,请对字母和数字字符使用\w*

我不理解您的示例。是否正确?myRegexFor()是您创建的函数?它做什么?抱歉,更新了一个问题问题问题中仍然不清楚您希望做什么。您列出的两个示例都包含字符串,但只有一个应该有效,为什么?但这应该返回
false
为什么应该返回false?它包含“/example/regex”。