JavaScript RegExp 如何在各種方括號間返回值?如何獲取值括號?


使用帶索引 0,1...N 的正則表示式獲取無括號的值。以下是程式碼 −

示例

var regularExpression= /(?<=\[).*?(?=\])/g;
var getTheValueWithIndex= "[John Smith][David
Miller]".match(regularExpression);
console.log("The first value without bracket="+getTheValueWithIndex[0]);
console.log("The second value without
bracket="+getTheValueWithIndex[1]);

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

node fileName.js.

在此,我的檔名是 demo132.js。

輸出

這將產生以下輸出 −

PS C:\Users\Amit\JavaScript-code> node demo132.js
The first value without bracket=John Smith
The second value without bracket=David Miller

更新於: 11-Sep-2020

1K+ 閱讀

開始你的 職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.