admin管理员组

文章数量:1355684

I have this:

.html?param_1

I want this:

iu4pa9rm8vfh
var url =".html?param_1";
  document.getElementById("demo").innerHTML = 
"The pathname of this page is :" + url.pathname;

Thanks in advance for any guidance!

update what is the problem in results

<!DOCTYPE html>
<html>

<body>

  <p id="demo"></p>

  <script>
    const url = new URL(".html?param_1")
    const pathname = url.pathname.match(/([0-9a-z]+)/)
    console.log(pathname[0])

    document.getElementById("demo").innerHTML = "The pathname of this page is :" + pathname;
  </script>

</body>

</html>

I have this:

http://example./iu4pa9rm8vfh.html?param_1

I want this:

iu4pa9rm8vfh
var url ="http://example./iu4pa9rm8vfh.html?param_1";
  document.getElementById("demo").innerHTML = 
"The pathname of this page is :" + url.pathname;

Thanks in advance for any guidance!

update what is the problem in results

<!DOCTYPE html>
<html>

<body>

  <p id="demo"></p>

  <script>
    const url = new URL("http://example./iu4pa9rm8vfh.html?param_1")
    const pathname = url.pathname.match(/([0-9a-z]+)/)
    console.log(pathname[0])

    document.getElementById("demo").innerHTML = "The pathname of this page is :" + pathname;
  </script>

</body>

</html>

Share Improve this question edited Jan 29, 2020 at 12:55 BENKHALDOUN asked Jan 29, 2020 at 12:04 BENKHALDOUNBENKHALDOUN 878 bronze badges 3
  • 1 developer.mozilla/en-US/docs/Web/API/URL – Teemu Commented Jan 29, 2020 at 12:06
  • 1 search for ' string ' methods. You will find there what you need. – Mihai T Commented Jan 29, 2020 at 12:07
  • 1 pathname is more a part of file name ... – Nina Scholz Commented Jan 29, 2020 at 12:08
Add a ment  | 

5 Answers 5

Reset to default 4

You can use the URL constructor to build a URL object, like so:

const url = new URL("http://example./iu4pa9rm8vfh.html?param_1")
console.log(url.pathname)

Then strip the bits you don't want out. Or rather, in this example below, perform a regex on the pathname to retrieve only the alphanumeric characters:

const url = new URL("http://example./iu4pa9rm8vfh.html?param_1")
const pathname = url.pathname.match(/([0-9a-z]+)/)
console.log(pathname[0])

Note that if you supply an invalid URL to the URL constructor then it will throw an error, so make sure you catch that and handle it gracefully.

More info on URL:

  • see this question/answer: https://stackoverflow./a/31415172/2039244
  • or read the MDN docs: https://developer.mozilla/en-US/docs/Web/API/URL/URL)

As you tagged this question with the jquery tag (perhaps you require an answer that works in <=IE11 as these browsers do not support the URL constructor), then here is an approach you can use:

function parseUrl(url) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

const url = parseUrl('http://example./iu4pa9rm8vfh.html?param_1')
const pathname = url.pathname.match(/([0-9a-z]+)/)
console.log(pathname[0])

(Modified from this answer.)

you can do it like this :

const url = new URL('http://example./iu4pa9rm8vfh.html?param_1')
let path = url.pathname.split('.')[0].replace('/','')
document.getElementById("demo").innerHTML = "The pathname of this page is :" + path;

<!DOCTYPE html>
<html>

<body>

  <p id="demo"></p>

  <script>
    const url = new URL('http://example./iu4pa9rm8vfh.html?param_1')
    let path = url.pathname.split('.')[0].replace('/','')
    document.getElementById("demo").innerHTML = "The pathname of this page is :" + path;
  </script>

</body>

</html>

There are a number of ways in which you can fetch out the pathname.

But technically speaking you should use the URL constructor method to create an object out of the URL. And then use its property to fetch the pathname.

Property name: pathname

const url = new URL("http://example./iu4pa9rm8vfh.html?param_1")
console.log(url.pathname.replace("/", "").replace(".html",""))

I have used the replace method to remove the slash and .html tag present in the pathname. I hope it helps! :)

Use the Javascript URL Api if possible: https://developer.mozilla/en-US/docs/Web/API/URL_API

let addr = new URL("http://example./iu4pa9rm8vfh.html?param_1");
// addr.pathname will return "/iu4pa9rm8vfh.html"
// Many wasys to skin this cat, but below is one way using split and slice
// Result will be "iu4pa9rm8vfh"
console.log(addr.pathname.split('.')[0].slice(1));

You can do it on a variety of different ways but I would like to suggest you to use subString(startIndex,endIndex).

Following code will return any thing between ./ and .html

var url = "http://example./iu4pa9rm8vfh.html?param_1";

url.substring(url.indexOf('./')+5,url.indexOf('.html'));

本文标签: jqueryHow can I get pathname values from url in JavaScriptStack Overflow