remove text from url string javascript - Stack Overflow

I want to display, with javascript, just the filename of the page the user is on. For example,.htmlshou

I want to display, with javascript, just the filename of the page the user is on. For example,

.html

should return

wow

I was trying to use the replace() method, like so:

var url = document.URL;
document.getElementById("code").innerHTML = url.replace("/", " ");

But I can't figure out how to remove the .html extension as well, or also replace https urls. Is there a better way to do this?

Thanks.

I want to display, with javascript, just the filename of the page the user is on. For example,

https://test.example./wow.html

should return

wow

I was trying to use the replace() method, like so:

var url = document.URL;
document.getElementById("code").innerHTML = url.replace("http://test.example./", " ");

But I can't figure out how to remove the .html extension as well, or also replace https urls. Is there a better way to do this?

Thanks.

Share Improve this question asked Jul 6, 2015 at 19:37 deanboysupremedeanboysupreme 872 silver badges13 bronze badges 3
  • after the replace you could do a substring upto the indexOf .html – depperm Commented Jul 6, 2015 at 19:40
  • stackoverflow./a/3671574/1267304 – DontVoteMeDown Commented Jul 6, 2015 at 19:41
  • possible duplicate of REGEX: Capture Filename from URL without file extention – DontVoteMeDown Commented Jul 6, 2015 at 19:42
Add a ment  | 

5 Answers 5

Reset to default 2

Another possible way to do it.

var url = "https://test.example./wow.html"

var urlSplit = url.split("/");
var name = urlSplit[urlSplit.length-1].split(".")[0];
console.log(name);

// this will fail of course with a name like my.page.html but this is just to give another alternative. :)

There's a little hack using an <a> element that works as such:

var parser = document.createElement('a');
parser.href = "http://example./foo.html";
parser.pathname; // => "/foo.html"

You can parse off the / and the .html however you want, but you'll always get the correct path back from .pathname.

See here for more info about this method.

This might work for you:

document.getElementById("code").innerHTML = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));

You probably want to use regular expressions, like this:

"https://test.example./wow.html".match(/https?:\/\/test\.example\.\/(\w*).html/)
// Returned list: ["https://test.example./wow.html", "wow"]

When starting out a tool like Regex101 can be useful to make sense of the expression:

var text = "https://test.example./wow.html";
var new_text = text.slice(text.indexOf("//") + 2, text.indexOf(".html")).split('/');
console.log(new_text[1]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744978878a4604315.html

相关推荐

  • remove text from url string javascript - Stack Overflow

    I want to display, with javascript, just the filename of the page the user is on. For example,.htmlshou

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信