-4

I have a few URL's like these

  • https://{Domain}/rent/abcdef/2019/Canada
  • https:/{Domain}/buy/Supported/2019/Gill-Avenue

I want to remove '2019'or any part which contain only numbers from these Url's so that the Url's look as below

  • https://{Domain}/rent/abcdef/Canada

  • https:/{Domain}/buy/Supported/Gill-Avenue

How can i achieve this using javascript

2
  • 2
    You don't want jQuery for this as it's primarily a framework for manipulating the DOM. You want JS, and specifically Regular Expressions. Commented Aug 9, 2019 at 13:34
  • here's a regular expression tutorial: regular-expressions.info Commented Aug 9, 2019 at 13:43

3 Answers 3

4

You can try this;

let str = "https://test.com/rent/abcdef/2019/Canada";
str.replace(/\/\d+/g, '');
Sign up to request clarification or add additional context in comments.

3 Comments

Consider adding the global flag to support cases where a URL has more than one set of numbers /\/\d+/g.
It is required if the text contains more than number part. thanks @Grafluxe
Almost. You have to define a variable to str.replace(), or change str to its replacement. Check my answer.
1

You should try something like that: split on '/', filter with a /d regex and rejoin with '/'

I can't try right now sorry

window.location.href.split('/').filter(substr => !(/^\d+$/.match(substr))).join('/')

2 Comments

You can simply use the replace function to solve this problem without having to call 4 separate functions. It would be faster, cleaner, and more simple to understand.
"I can't try right now sorry" What do you mean by that?
0

Try to do this for the first:

var str = "https://example.com/rent/abcdef/2019/Canada"
str = str.replace(/[0-9]/g, '');
str = str.replace("f//", "f/");

And for the second:

var str = "https://example.com/rent/abcdef/2019/Canada"
str = str.replace(/[0-9]/g, '');
str = str.replace("d//", "d/");

So this is if you want to replace just 1 digit. The first one of each of these works but adds a new / backslash to the whole link after the last letter before the / in the old version. To remove that, you do the second, which contains the last letter to not remove the :// too. The way is to find the last letter of each of these numbers before the backslash after using the first replace() function and replace them to remove the extra backslash.

This might work for easy things, like if you already know the URL, but for complicated things like a very big project, this is no easy way to do it. If you want "easy", then check other answers.

As said, you can also do this:

let str = "https://test.com/rent/abcdef/2019/Canada";
var withNoNum = str.replace(/\/\d+/g, '');

This is going to remove groups of numbers. So I added a new string withNoNum which is str's replacement with no numbers, which might be more good because if you are doing a website that allows you to send your own website and remove the numbers from it to get a new site.

This also might help you with this problem: removing numbers from string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.