-2

I'm making a SPA in Laravel and jQuery. The URL string is domain.com/company/page. As company is obligatory and variable, i wonder how i can edit /page part only? I make use of window.PushState to modify the URL without reloading.

My first guess would involve RegEx, i'm however not too familiar with RegEx, which makes this task kind of tricky. The hard part, is editing the second part of the URL only, as company can vary.

Cheers :)

StevenThelin
  • 235
  • 1
  • 3
  • 10
  • What is it you wish to do ? Search for `/company/page` part and replace it with some other string ? –  Jan 23 '16 at 09:30
  • I want to replace the "page" part, with the given page from the referer, whilst keeping the company parameter :) Eg. /home, /stuff, /otherstuff – StevenThelin Jan 23 '16 at 09:33
  • *I'm however not too familiar with RegExp.* Why not read up on it? –  Jan 23 '16 at 12:12

1 Answers1

0

If you only wish to search and replace the /page then following is the simplest regex to do so:

/\/[A-Za-z]+$/

It searches for / followed by any number of alphabets ranging from A-Za-z which appears at the end of the string( your URL ).

Simplest demo here.
If you wish to enumerate the search and replace for multiple links use loop and array or object. Something like this.

  • 1
    You seem to like `[A-z]`, you are using it in your answers. But do you know what it can match? *alphabets ranging from `A-Za-z`* is not correct. See [\[A-z\] and \[a-zA-Z\] difference](http://stackoverflow.com/questions/4923380/difference-between-regex-a-z-and-a-za-z) – Wiktor Stribiżew Jan 23 '16 at 14:01
  • @WiktorStribiżew: Thanx for pointing that out. Am a complete noob ! –  Jan 23 '16 at 14:06