जवाबों:
var filename = fullPath.replace(/^.*[\\\/]/, '')
यह दोनों पथों में \ या को हैंडल करेगा
replaceमें बहुत धीमा है substr, जिसका उपयोग इसके साथ किया जा सकता है lastIndexOf('/')+1: jsperf.com/replace-vs-substring
Fiddle, हालांकि `\\` के लिए , यह ठीक काम करता है।
"/var/drop/foo/boo/moo.js".replace(/^.*[\\\/]/, '')रिटर्नmoo.js
प्रदर्शन के लिए, मैंने यहाँ दिए गए सभी उत्तरों का परीक्षण किया:
var substringTest = function (str) {
return str.substring(str.lastIndexOf('/')+1);
}
var replaceTest = function (str) {
return str.replace(/^.*(\\|\/|\:)/, '');
}
var execTest = function (str) {
return /([^\\]+)$/.exec(str)[1];
}
var splitTest = function (str) {
return str.split('\\').pop().split('/').pop();
}
substringTest took 0.09508600000000023ms
replaceTest took 0.049203000000000004ms
execTest took 0.04859899999999939ms
splitTest took 0.02505500000000005ms
और विजेता स्प्लिट और पॉप शैली का उत्तर है, बोबिन्स के लिए धन्यवाद !
path.split(/.*[\/|\\]/)[1];
Node.js में, आप पाथ के पार्स मॉड्यूल का उपयोग कर सकते हैं ...
var path = require('path');
var file = '/home/user/dir/file.txt';
var filename = path.parse(file).base;
//=> 'file.txt'
basenameफ़ंक्शन का उपयोग कर सकते हैं :path.basename(file)
रास्ता किस प्लेटफार्म से आता है? Windows पथ POSIX पथ से भिन्न हैं, OS OS से भिन्न हैं 9 पथ RISC से भिन्न हैं OS पथ भिन्न हैं ...
यदि यह एक वेब ऐप है जहां फ़ाइलनाम विभिन्न प्लेटफार्मों से आ सकता है तो कोई भी समाधान नहीं है। हालाँकि, पथ प्रदर्शक के रूप में '\' (विंडोज़) और '/' (लिनक्स / यूनिक्स / मैक और विंडोज पर एक विकल्प) दोनों का उपयोग करने के लिए एक उचित स्टैब है। यहाँ अतिरिक्त मज़ा के लिए एक गैर- RegExp संस्करण है:
var leafname= pathname.split('\\').pop().split('/').pop();
var path = '\\Dir2\\Sub1\\SubSub1'; //path = '/Dir2/Sub1/SubSub1'; path = path.split('\\').length > 1 ? path.split('\\').slice(0, -1).join('\\') : path; path = path.split('/').length > 1 ? path.split('/').slice(0, -1).join('/') : path; console.log(path);
Ates, आपका समाधान इनपुट के रूप में एक रिक्त स्ट्रिंग से सुरक्षा नहीं करता है। उस स्थिति में, यह विफल रहता है TypeError: /([^(\\|\/|\:)]+)$/.exec(fullPath) has no properties।
bobince, यहाँ nickf का एक संस्करण है जो DOS, POSIX, और HFS पथ परिसीमन (और खाली तार) को संभालता है:
return fullPath.replace(/^.*(\\|\/|\:)/, '');
निक के उत्तर की तुलना में अधिक संक्षिप्त नहीं है , लेकिन यह एक खाली स्ट्रिंग के साथ अवांछित भागों को बदलने के बजाय सीधे "अर्क" देता है:
var filename = /([^\\]+)$/.exec(fullPath)[1];
और एक
var filename = fullPath.split(/[\\\/]/).pop();
यहाँ एक चरित्र वर्ग के साथ विभाजन की एक नियमित अभिव्यक्ति है
दो वर्णों को '\' के साथ भाग जाना है
या विभाजित करने के लिए सरणी का उपयोग करें
var filename = fullPath.split(['/','\\']).pop();
यह जरूरत पड़ने पर किसी विभाजक को गतिशील रूप से धकेलने का तरीका होगा।
यदि fullPathआपके कोड में स्पष्ट रूप से एक स्ट्रिंग द्वारा सेट किया गया है तो उसे बैकस्लैश से बचने की आवश्यकता है !
पसंद"C:\\Documents and Settings\\img\\recycled log.jpg"
<script type="text/javascript">
function test()
{
var path = "C:/es/h221.txt";
var pos =path.lastIndexOf( path.charAt( path.indexOf(":")+1) );
alert("pos=" + pos );
var filename = path.substring( pos+1);
alert( filename );
}
</script>
<form name="InputForm"
action="page2.asp"
method="post">
<P><input type="button" name="b1" value="test file button"
onClick="test()">
</form>
पूरा जवाब है:
<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>
विंडोज के लिए एक पूर्ण पथ के साथ-साथ 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 showSrc() {
document.getElementById("myframe").href = document.getElementById("myfile").value;
var theexa = document.getElementById("myframe").href.replace("file:///","");
alert(document.getElementById("myframe").href.replace("file:///",""));
}
// -->
</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>
सफलतापूर्वक अपने प्रश्न के लिए स्क्रिप्ट, पूर्ण परीक्षण
<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)
}
यह समाधान 'फ़ाइल नाम' और 'पथ' दोनों के लिए बहुत सरल और सामान्य है।
const str = 'C:\\Documents and Settings\\img\\recycled log.jpg';
// regex to split path to two groups '(.*[\\\/])' for path and '(.*)' for file name
const regexPath = /^(.*[\\\/])(.*)$/;
// execute the match on the string str
const match = regexPath.exec(str);
if (match !== null) {
// we ignore the match[0] because it's the match for the hole path string
const filePath = match[1];
const fileName = match[2];
}
function getFileName(path, isExtension){
var fullFileName, fileNameWithoutExtension;
// replace \ to /
while( path.indexOf("\\") !== -1 ){
path = path.replace("\\", "/");
}
fullFileName = path.split("/").pop();
return (isExtension) ? fullFileName : fullFileName.slice( 0, fullFileName.lastIndexOf(".") );
}