JavaScript-拆分字符串 [英] JavaScript - Split A String

查看:53
本文介绍了JavaScript-拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,其值保持为'website.html'.

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

如何拆分该变量,使其仅提供网站"?

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

谢谢

推荐答案

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(".");

但是,如果文件名类似于 my.old.file.tar.gz ,则它将认为 my.old.file.tar 是文件名

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

这篇关于JavaScript-拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆