1

i am looking for create a regular exp in JavaScript to look for patterns like ${.............}. Lets say if have string like

{
      "type" : "id",
      "id" : ${idOf('/tar/check/inof/high1')},
      "details" : [
        {
          "name" : "Sanio",
          "sig" : "DA123QW"
        },
        {
          "name" : "Tarer",
          "sig" : "BAWE3QW"
        },
        {
          "name" : "Kadek",
          "sig" : "KSWE2J"
        },
      ]
    },

So i am searching for all patterns having ${whateverhere}

i am trying with

var pathRegExp = /\$(.*)\}/;

But it is not ending at "}" symbol.

2
  • What your expected result? Commented Mar 4, 2015 at 20:38
  • i am searching for patterns like "${idOf('/tar/check/inof/high1')}" and wanted to encode them in double qoutes for JSON conversion... Commented Mar 4, 2015 at 20:40

4 Answers 4

2

Match pattern

If you want to match ${...} pattern then you can use this regex:

\$\{.*?\}

Working demo

enter image description here

Match pattern and grab content

On the other hand, if you want to capture the content within ${...} you can use capturing groups like this:

\$\{(.*?)\}
    ^---^--- Capture here

Working demo

enter image description here

The match information would be:

MATCH 1
1.  [38-67] `idOf('/tar/check/inof/high1')`
MATCH 2
1.  [199-209]   `anotherOne`

If you take a look at the Code generator section you can find the javascript code for this:

var re = /\$\{(.*?)\}/g; 
var str = '{\n      "type" : "id",\n      "id" : ${idOf(\'/tar/check/inof/high1\')},\n      "details" : [\n        {\n          "name" : "Sanio",\n          "sig" : "DA123QW"\n        },\n        {\n          "name" : "${anotherOne}",\n          "sig" : "BAWE3QW"\n        },\n        {\n          "name" : "Kadek",\n          "sig" : "KSWE2J"\n        },\n      ]\n    },';
var m;

while ((m = re.exec(str)) != null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    // View your result using the m-variable.
    // eg m[0] etc.
}
Sign up to request clarification or add additional context in comments.

2 Comments

There could be multiple entries of "id". But this regular expression is not searching for all them...Any suggestions?
@gpapps you have to set the G (global) flag. I've just edited the answer
0

It sound's like you need to change it to something like this:

var pathRegExp = /\${(.*?)}/g;

Check it out here: Regex101

I made it become lazy, since Regex is greedy by default and was looking for the last } by default.

Comments

0

This is the regex you are looking for:

\$\{[^}]*\}

This way, it matches with the first ocurrence of '}', because [^}] means "match any character but '}'".

Comments

-1

You forgot the opening curly brace {

This should work:

/\$\{(.*)\}/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.