0

Need help in solving this regex issues, I am stuck at, and not able to solve. I am using nodejs 6.10

For example following are the incoming patterns

  • test
  • test123
  • test/

I need help creating a regex match so -
- only test is matched, and not test123 or test/ or
- for test123, it shall not prematch at test

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
It matches test and return 302 on /test-videos.html
on 302 when the request reaches for /test-videos.html again at the server
test is matched again, and a 302 is returned with /test-videos.html-videos.html,
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-videos.html-videos.html-videos.html-videos.html-videos.html-videos.html

Need help with an expression which matches test with nothing succeeding it.

Vivek Sharma
  • 3,794
  • 7
  • 38
  • 48

2 Answers2

0

You can use the $ to match the end of the string, e.g. '^test$'

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

Philip
  • 1,526
  • 1
  • 14
  • 25
0

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. From the Regex guide in MDN: 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. For example, /t$/ does not match the 't' in "eater", but does match it in "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)
Peter Grainger
  • 4,539
  • 1
  • 18
  • 22