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

JavascriptWeb DevelopmentObject Oriented Programming

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
raja
Published on 11-Sep-2020 06:17:53
Advertisements