shanmugharaj
shanmugharaj

Reputation: 3924

how to find the matching string between '{ } ' in node.js

My sample string is

var str = "My name is {name}.from {area}";

I need both {name} and {area} separately from that string.

I TRIED

var s = "My name is {name}.from {area}";
var matches = s.match(/\{(.*?)\}/);
console.log(matches);

It gives only the first occurence. How to get both the strings {name} {area}.

Upvotes: 2

Views: 2157

Answers (1)

Matt Cain
Matt Cain

Reputation: 5768

Add the global flag to the regular expression:

var matches = s.match(/\{(.*?)\}/g);

This will get you all of the matches of the regular expression, not just the first one.

Upvotes: 6

Related Questions