1

I am trying to find a pattern in a string that has a value that starts with ${ and ends with }. There will be a word between the curly brackets, but I won't know what word it is.

This is what I have \$\\{[a-zA-Z]\\}

${a} works, but ${aa} doesn't. It seems it's only looking for a single character.

I am unsure what I am doing wrong, or how to fix it and would appreciate any help anyone can provide.

António Almeida
  • 8,450
  • 5
  • 54
  • 59
Nick
  • 35
  • 4
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Heretic Monkey Jun 25 '15 at 17:32

4 Answers4

2

I think this could help you

var str = "The quick brown ${fox} jumps over the lazy ${dog}";

var re = /\$\{([a-z]+)\}/gi;

var match;

while (match = re.exec(str)) {
  console.log(match[1]);
}

Click Run code snippet and check your developer console for output

"fox"
"dog"

Explanation

  • + means match 1 or more of the previous term — in this example, match 1 or more of [a-z]
  • the (...) parentheses will "capture" the match so you can actually do something with it — in my example, I'm just using console.log to output it
  • the i modifier (at the end of the regexp) means perform a case-insensitive match
  • the g modifier means match all instances of this regexp in the target string
  • The while loop will continue running for each match that re.exec finds. Once re.exec cannot match another instance, it will return null and the loop will exit.

Additional information

Try console.log(match) using the code above. Each match comes with other useful information such as the string index where the match occurred

Gotchas

This will not work for nested ${} sets

For example, this regexp will not work on "The quick brown ${fox jumps ${over}} the lazy ${dog}."

Thank you
  • 107,507
  • 28
  • 191
  • 224
  • This will not work with capitals. Along with that the user seems to only be asking for the regex instead of a JS program. – Spencer Wieczorek Jun 25 '15 at 17:43
  • @SpencerWieczorek, I used the `i` modifier to perform a case-insensitive match. Also, I demonstrated usage with a javascript program to show that the `g` modifier was also probably necessary otherwise he'd probably only get one match. In this case, I think the simple program is helpful as it provides a complete demonstration of functionality. With your answer, he will likely hit another dead-end in a very short while. – Thank you Jun 25 '15 at 17:47
  • How so? My answer is correct and gives exactly what was asked for, the question stated that only a word would be between `${...}`, the nested case you mentioned would never occur. You are assuming that the OP knows nearly nothing. – Spencer Wieczorek Jun 25 '15 at 18:01
1

A good website for regex reference and testing is http://rubular.com/

It looks like you need to add a +, which tells the regex to look for one or more of a character.

Try: \${[a-zA-Z]+}

Ian Agne
  • 140
  • 4
1

You're close!

All you need is to use a + to tell the expression that there will be one or more of whatever was just before it (in this case [a-zA-Z]) like this:

\${[a-zA-Z]+}
  • @MaxZoom Because you are using a hyphen and a digit (number). The ranges specified are `a` through `z` and `A` through `Z`. There are no hyphens or numbers in those ranges. Digits can be added using `0-9`. A hyphen can be added escaped. The whole thing might look like this `[a-zA-Z0-9\-]`. –  Jun 25 '15 at 18:45
0

You need to use * (zero or more) or + (one or more). So this [a-zA-Z] would be [a-zA-Z]+, meaning 1 or more letters. The entire regex would look like:

\$\{[a-zA-Z]+\}
Spencer Wieczorek
  • 19,788
  • 7
  • 39
  • 50
  • Why the downvote? This answer is correct, was the first answer. Two other answers are essentially copies of this one, yet this one is downvoted? – Spencer Wieczorek Jun 26 '15 at 04:14