JavaScript RegExp 返回各种方括号之间的值?如何获取括号中的值?

javascriptweb developmentobject oriented programming更新于 2025/11/15 17:16:17

使用索引为 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

相关文章


有用资源