Replacing everything with Javascript

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

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

With javascript, I use this code to change the url, but it leaves an identifier.

<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 it behind 7NGOWX 7iTAXM?

+4
source share
3 answers

You need to use a little regular expression syntax:

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

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

You can use a regular expression, for example /example\.com\/\w+/, \w+corresponding to any alphanumeric continuous way.

+1

URL() .

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

Source: https://habr.com/ru/post/1686089/


All Articles