JavaScript - Split A String

前端 未结 3 1910
别跟我提以往
别跟我提以往 2021-01-11 14:36

I have a variable that holds the value \'website.html\'.

How can I split that variable so it only gives me the \'website\'?

Thanks

3条回答
  •  暖寄归人
    2021-01-11 14:49

    var a = "website.html";
    var name = a.split(".")[0];
    

    If the file name has a dot in the name, you could try...

    var a = "website.old.html";
    var nameSplit = a.split(".");
    nameSplit.pop();    
    var name = nameSplit.join(".");
    

    But if the file name is something like my.old.file.tar.gz, then it will think my.old.file.tar is the file name

提交回复
热议问题