JavaScript RegExp return values between various square brackets? How to get value brackets?


Use regular expression with indexes 0,1….N to get the value without brackets. Following is the code −

Example

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]);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo132.js.

Output

This will produce the following output −

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

Updated on: 11-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements