在 JavaScript 中的副檔名前面插入一個詞?


假設以下為我們的檔名 -

var actualJavaScriptFileName = "demo.js";

以下是要在點前插入的單詞副檔名 -

var addValueBetweenFileNameAndExtensions = "programming";

首先,您需要根據點(.)拆分檔名,然後插入一個字元,您可以使用模板變數的概念。以下是程式碼 -

示例

var addValueBetweenFileNameAndExtensions = "programming";
var actualJavaScriptFileName = "demo.js";
console.log("The actual File name="+actualJavaScriptFileName);
var [fileName, fileExtension] = actualJavaScriptFileName.split('.');
console.log("After adding into the file name=");
console.log(`${fileName}-
${addValueBetweenFileNameAndExtensions}.${fileExtension}`)

要執行上面的程式,您需要使用以下命令 -

node fileName.js.

輸出

此處,我的檔名是 demo124.js。這將產生以下輸出 -

PS C:\Users\Amit\JavaScript-code> node demo.js
The actual File name=demo.js
After adding into the file name=
demo-programming.js

更新於: 2020 年 9 月 10 日

398 瀏覽

開啟你的 職業

完成課程認證

開始
廣告
© . All rights reserved.