繁体   English   中英

正则表达式匹配精确的字符串-进入循环

[英]Regex match on exact string - getting into a loop

在解决此正则表达式问题方面需要帮助,但我仍无法解决。 我正在使用nodejs 6.10

例如,以下是传入模式

  • 测试
  • 测试123
  • 测试/

我需要创建正则表达式匹配项的帮助,所以-
- 仅匹配测试而不 匹配 test123或test /
-对于test123,它在测试时不得预先匹配

目前,我正在使用以下规则进行重定向

‘^/test /test-videos.html [R=302,L]’  
'^/test/ /test/videos.html [R=302,L]’  
'^/test123 /test/test-videos.html [R=302,L]’

获取www.domain.com/test
它与test匹配并在/test-videos.html上返回302
当请求再次到达服务器上的/test-videos.html时在302上
测试再次匹配,并且/test-videos.html-videos.html返回302,
并在该请求上再次相同,因此它陷入了一个无限循环
/test-videos.html-videos.html-videos.html
/test-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html

需要一个与test匹配但之后没有匹配的表达式的帮助。

您可以使用$来匹配字符串的结尾,例如'^ test $'

查看http://regular-expressions.mobi/anchors.html?wlr=1

你快到了。 您可以使用^来匹配字符串的开头。 你只需要添加一个$到后输入结束匹配test 从MDN中的Regex指南中: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dollar

$匹配输入的结尾。 如果将多行标志设置为true,则也将在换行符前立即匹配。 例如,/ t $ /与“ eater”中的“ t”不匹配,但与“ eat”中的匹配。

 const testRegex = /^test$/; const justTest = testRegex.test('test'); const trailingSlash = testRegex.test('test/'); const numbers = testRegex.test('test123'); console.log('match test', justTest) console.log('trailing slash', trailingSlash) console.log('numbers', numbers) 

暂无
暂无

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

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