-2

I want to write a javascript regular expression to match URLs which have the word -booker in it.

URLs are generally like:

http://domain.local/htmlforms/mforms/product1-booker.html
http://domain.local/htmlforms/mforms/product2-booker.html
http://domain.local/htmlforms/mforms/product3.html
http://domain.local/htmlforms/mforms/product4.html

Thank you.

Madav
  • 311
  • 1
  • 5
  • 12
  • 1
    have you tried anything? start reading regex from here :- http://stackoverflow.com/questions/4736/learning-regular-expressions – rock321987 Apr 05 '16 at 12:16

3 Answers3

1

This should do it:

https?\:\/{2}[\w\.]+(\/[\w]+)+\-booker(\/[\w]+|\.[\w]+)

This will match

  • http with an optional s.
  • two forward slashes.
  • A word like item, or a full stop one or more times.
  • A forward slash or word item one or more times
  • -booker one time
  • A forward slash or word item one or more times (second time, incase -booker is in the middle, this option can be removed if needed).
  • A full stop and a word like character one or more times (for the file name.)

For an example, see here.

I hope this helps.

KyleFairns
  • 2,947
  • 1
  • 15
  • 35
0

Also, please try:

/(?=https?:\/\/[^ ]+-booker[^ ]+)(.*)/g

(?=https?:\/\/[^ ]*-booker[^ ]+) is a Positive Lookahead - Assert that word -booker should be found in the url.

REGEX 101 DEMO

Quinn
  • 4,394
  • 2
  • 21
  • 19
-1

I think this will help

/\http:\/\/domain.local\/htmlforms\/mforms\/\w+-booker.html/g
Wilder Pereira
  • 2,249
  • 3
  • 21
  • 31