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.