Herbo
Herbo

Reputation: 155

Replacing everything with Javascript

Currently I have the following structure (the ID at the end changes based on the code given by the shortner)

example.com/7NGOWX
example.com/7iTAXM

With javascript I am current using this code to change the url but it leaves the id.

<script>
    document.body.innerHTML = document.body.innerHTML.replace(/example.com/g, '');
</script>

How can I make it so that it removes the entire url instead of leaving things like 7NGOWX 7iTAXM behind?

Upvotes: 4

Views: 41

Answers (3)

Andrew L
Andrew L

Reputation: 5486

Another option can be to use the URL() constructor and have it do the work.

for example

var url = new URL("http://example.com/7iTAXM");
console.log(url.pathname.substring(1));

Upvotes: 1

Duc Filan
Duc Filan

Reputation: 7157

You need to use a little regex syntax:

example\.com\/\w+
  • \w+ matches any word character (equal to [a-zA-Z0-9_])

<script>
    document.body.innerHTML = document.body.innerHTML.replace(/example\.com\/\w+/g, '');
</script>

Upvotes: 3

&#214;zcan Esen
&#214;zcan Esen

Reputation: 340

You can use regex like /example\.com\/\w+/ , \w+ match any alphanumerical continuous word.

Upvotes: 1

Related Questions