sourceConfigPath変数には、のようなパスが"conf/test.json"
あるか、のような別のレイヤーがある場合があり"test/conf/test.json"
ます。"test.json"
パーツだけを入手したいです。
indexOf関数を試して位置を取得してから、slice関数またはsubstr関数を使用して'test.json'
パーツを取得しました。ただし、実行すると常に0が返されますindexOf
。
誰かがここで助けてくれますか?どうもありがとう!
var position = sourceConfigPath.indexOf('conf');
var newsourceConfigPath = sourceConfigPath.slice(position+4);
または、これを行うためのより良い方法はありますか?どうもありがとう!
最良の方法はpath.basenameを使用することです
path.basename()メソッドは、Unixのベース名と同様に、パスの最後の部分を返します。
const path = require('path');
const newSource = path.basename('conf/test.json'); // test.json
のlastIndexOf
代わりに使用できますがindexOf
、path.basename
お勧めします。
const filepath = '/path/to/file.json';
const position = filepath.lastIndexOf('/') + 1; // +1 is to remove '/'
console.log(filepath.substr(position));
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加