是否有一种方法,我可以得到的最后一个值(基于'\'符号)从一个完整的路径?
例子:
C:\Documents and Settings\img\recycled log.jpg
在这种情况下,我只想从JavaScript的完整路径中回收log.jpg。
是否有一种方法,我可以得到的最后一个值(基于'\'符号)从一个完整的路径?
例子:
C:\Documents and Settings\img\recycled log.jpg
在这种情况下,我只想从JavaScript的完整路径中回收log.jpg。
当前回答
在您的项目中包含一个小函数,用于从Windows的完整路径以及GNU/Linux和UNIX的绝对路径中确定文件名。
/**
* @param {String} path Absolute path
* @return {String} File name
* @todo argument type checking during runtime
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
* @example basename('/home/johndoe/github/my-package/webpack.config.js') // "webpack.config.js"
* @example basename('C:\\Users\\johndoe\\github\\my-package\\webpack.config.js') // "webpack.config.js"
*/
function basename(path) {
let separator = '/'
const windowsSeparator = '\\'
if (path.includes(windowsSeparator)) {
separator = windowsSeparator
}
return path.slice(path.lastIndexOf(separator) + 1)
}
其他回答
完整的答案是:
<html>
<head>
<title>Testing File Upload Inputs</title>
<script type="text/javascript">
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'),with_this);
}
function showSrc() {
document.getElementById("myframe").href = document.getElementById("myfile").value;
var theexa = document.getElementById("myframe").href.replace("file:///","");
var path = document.getElementById("myframe").href.replace("file:///","");
var correctPath = replaceAll(path,"%20"," ");
alert(correctPath);
}
</script>
</head>
<body>
<form method="get" action="#" >
<input type="file"
id="myfile"
onChange="javascript:showSrc();"
size="30">
<br>
<a href="#" id="myframe"></a>
</form>
</body>
</html>
Ates,您的解决方案不能防止空字符串作为输入。在这种情况下,它失败的TypeError: /([^(\\|\/|\:)]+)$/.exec(fullPath)没有属性。
这是nickf's的一个版本,它处理DOS, POSIX和HFS路径分隔符(和空字符串):
return fullPath.replace(/^.*(\\|\/|\:)/, '');
在您的项目中包含一个小函数,用于从Windows的完整路径以及GNU/Linux和UNIX的绝对路径中确定文件名。
/**
* @param {String} path Absolute path
* @return {String} File name
* @todo argument type checking during runtime
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
* @example basename('/home/johndoe/github/my-package/webpack.config.js') // "webpack.config.js"
* @example basename('C:\\Users\\johndoe\\github\\my-package\\webpack.config.js') // "webpack.config.js"
*/
function basename(path) {
let separator = '/'
const windowsSeparator = '\\'
if (path.includes(windowsSeparator)) {
separator = windowsSeparator
}
return path.slice(path.lastIndexOf(separator) + 1)
}
不需要专门处理反斜杠;大多数答案不处理搜索参数。
现代的方法是简单地使用URL API并获取pathname属性。API将反斜杠规范化为斜杠。注意,位置(在浏览器环境中)也可以工作,但仅适用于当前URL,而不是任意URL。
为了将结果%20解析为空格,只需将其传递给decodeURIComponent。
const getFileName = (fileName) => new URL(fileName).pathname.split("/").pop(); // URLs need to have the scheme portion, e.g. `file://` or `https://`. console.log(getFileName("file://C:\\Documents and Settings\\img\\recycled log.jpg")); // "recycled%20log.jpg" console.log(decodeURIComponent(getFileName("file://C:\\Documents and Settings\\img\\recycled log.jpg"))); // "recycled log.jpg" console.log(getFileName("https://example.com:443/path/to/file.png?size=480")); // "file.png" .as-console-wrapper { max-height: 100% !important; top: 0; }
如果你总是想要路径的最后一个非空部分(例如,来自https://example.com/file.png/的file.png),在.pop()之前添加一个.filter(布尔值)。
如果您只有一个相对URL,但仍然只想获得文件名,则使用URL构造函数的第二个参数来传递基源。“https://example.com”足够:新的URL(文件名,“https://example.com”)。也可以在你的文件名前加上“https://”——URL构造函数接受https://path/to/file.ext作为有效的URL。
成功为你的问题编写脚本,完整测试
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<p title="text" id="FileNameShow" ></p>
<input type="file"
id="myfile"
onchange="javascript:showSrc();"
size="30">
<script type="text/javascript">
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'), with_this);
}
function showSrc() {
document.getElementById("myframe").href = document.getElementById("myfile").value;
var theexa = document.getElementById("myframe").href.replace("file:///", "");
var path = document.getElementById("myframe").href.replace("file:///", "");
var correctPath = replaceAll(path, "%20", " ");
alert(correctPath);
var filename = correctPath.replace(/^.*[\\\/]/, '')
$("#FileNameShow").text(filename)
}