0

Learning regexp but currently I'm a bit stucked with such pattern..

I need to match my string inside url string, example:

If url contains string '/example/regex' it should return true.

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

Thanks!

4
  • I don't understand your example. Is it right? Commented May 27, 2013 at 14:31
  • myRegexFor() is a function you created? What does it do? Commented May 27, 2013 at 14:32
  • sorry, updated a question Commented May 27, 2013 at 14:38
  • It still isn't clear in the question what you wish to do. Both the examples you listed contains the string, but only one should be valid, why? Commented May 27, 2013 at 14:42

2 Answers 2

1

in your case using $ you can indicate the end of the regex

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

if you don't want any slash more use \w* for letters and numbers characters

Sign up to request clarification or add additional context in comments.

Comments

0

You just escape the backslash and you need to clean it a bit;

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

According to what you gave as examples, both should validate, though.

2 Comments

but this should return false
Why should this return false? It contains '/example/regex'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.