0

I need to write a JavaScript RegEx that matches in partial strings of an URL.

Example: Every partial match for "www.url.com/foo"

www.url.com/foo/bar/send/123   - true
www.url.com/foo/doe/get/123    - true
www.url.co/foo/doe/get/123     - false
http://www.url.com/foo/get/123 - true
http://www.url.co/foo/doe/123  - false

How can I do an regex that uses slashs in string?

Jhonathan
  • 330
  • 1
  • 2
  • 14
  • You may want to have a look at : http://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url – Bettorun Mar 22 '17 at 13:53
  • *"How can I do an regex that uses slashs in string?"* If that's ultimately where you're stuck, you escape the slash. `/foo\/bar/` –  Mar 22 '17 at 13:53
  • `var r = new RegExp('www.url.com\/foo')` `r.test("www.url.com/foo/bar/send/123")` – Karthik VU Mar 22 '17 at 13:53
  • @KarthikVU: No need to escape the slash when building the regex from a string. It's only needed in the literal syntax in order to not indicate the end of the regex. And if an escape is needed, you'll need two backslashes. –  Mar 22 '17 at 13:55

0 Answers0