简体   繁体   English

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

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

Need help in solving this regex issues, I am stuck at, and not able to solve. 在解决此正则表达式问题方面需要帮助,但我仍无法解决。 I am using nodejs 6.10 我正在使用nodejs 6.10

For example following are the incoming patterns 例如,以下是传入模式

  • test 测试
  • test123 测试123
  • test/ 测试/

I need help creating a regex match so - 我需要创建正则表达式匹配项的帮助,所以-
- only test is matched , and not test123 or test/ or - 仅匹配测试而不 匹配 test123或test /
- for test123, it shall not prematch at test -对于test123,它在测试时不得预先匹配

Presently I am using the following rules for redirect 目前,我正在使用以下规则进行重定向

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

GET www.domain.com/test 获取www.domain.com/test
It matches test and return 302 on /test-videos.html 它与test匹配并在/test-videos.html上返回302
on 302 when the request reaches for /test-videos.html again at the server 当请求再次到达服务器上的/test-videos.html时在302上
test is matched again, and a 302 is returned with /test-videos.html-videos.html, 测试再次匹配,并且/test-videos.html-videos.html返回302,
and again the same on that request, so it gets into an unending loop of 并在该请求上再次相同,因此它陷入了一个无限循环
/test-videos.html-videos.html-videos.html /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-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html

Need help with an expression which matches test with nothing succeeding it. 需要一个与test匹配但之后没有匹配的表达式的帮助。

You can use the $ to match the end of the string, eg '^test$' 您可以使用$来匹配字符串的结尾,例如'^ test $'

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

You are nearly there. 你快到了。 You have the ^ for matching the start of the string. 您可以使用^来匹配字符串的开头。 You just need to add a $ to match the end of an input after the test . 你只需要添加一个$到后输入结束匹配test From the Regex guide in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dollar 从MDN中的Regex指南中: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dollar

$ Matches end of input. $匹配输入的结尾。 If the multiline flag is set to true, also matches immediately before a line break character. 如果将多行标志设置为true,则也将在换行符前立即匹配。 For example, /t$/ does not match the 't' in "eater", but does match it in "eat". 例如,/ 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