0

i am trying to remove a parameter from url with regexp and i keep getting Uncaught SyntaxError: Unexpected token

    var url=window.location.href;

    //Remove p first
    url = url.replace(/p/([0-9]+)/, '');

i am trying to remove the p parapemeter /p/*

my testing url is http://mycompany.com/en/category/p/5

What am i doing wrong

Thanks

1
  • 1
    You need to quote the slash: /p\/([0-9]+)/ Commented Jan 12, 2019 at 11:12

1 Answer 1

1

Apart of prepending the / with a backslash (as it was stated in a comment to your post), another hint: As you want only to delete the matched string, the capturing group here is not needed.

So change your code to:

url = url.replace(/p\/[0-9]+/, '');

Or even shorter option:

url = url.replace(/p\/\d+/, '');
Sign up to request clarification or add additional context in comments.

Comments

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.