在 JavaScript 中使用分隔符分割單詞後的字串


要使用分隔符分割單詞後的字串,語法如下 −

var anyVariableName=yourVariableName.split('parameter').filter(value=>value)

不妨說,以下是我們在分隔符內運用的字串 −

var sentence="-My-Name-is-John-Smith-I-live-in-US";

現在,使用以下程式碼中的分隔符分割字串。

示例

var sentence="-My-Name-is-John-Smith-I-live-in-US";
console.log("The value="+sentence);
var result=sentence.split('-').filter(value=>value)
console.log("After split()=");
console.log(result);

要執行以上程式,需要使用以下命令 −

node fileName.js.

這裡,我的檔名是 demo63.js。

輸出

將生成以下輸出 −

PS C:\Users\Amit\JavaScript-code> node demo63.js
The value=-My-Name-is-John-Smith-I-live-in-US
After split()=
[
   'My', 'Name',
   'is', 'John',
   'Smith', 'I',
   'live', 'in',
   'US'
]

更新於: 03-Sep-2020

218 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始使用
廣告