-1

I'm searching for a RegExp that finds {}

I was under the understanding that this: /[{}]{2}/g;

  • regexpression finds: {} and only the brackets if they stand exactly like that next to each other. I'm doing some code that makes me believe that I misunderstood that.

I now have this line: {{x,y},{0.0,1.0},{0.2,1.2214},{0.4,1.49182},{0.6,1.82212},{0.8,2.22554},{1.0,2.71828}}

And it becomes: { }x,y},{0.0,1.0},{0.2,1.2214},{0.4,1.49182},{0.6,1.82212},{0.8,2.22554},{1.0,2.71828{ }

That wasn't what I wanted, the above line should not be influenced by my regexp, because nowhere there is a "{}".

+++

ok so i misunderstood, How to do a rexeprexstion that ONLY findes "{}" when they stand exactly like that, no space between them, } comes after { ???

  • 1
    `[{}]{2}` is the same as `[{}][{}]` - two occurences of either brace. You're looking for `\{\}`. – Bergi Jul 26 '19 at 17:00
  • `[{}]` is a character class - either of the two elements will match. Depending on your regex flavour, you can do `{}` or `\{\}` or even `[{][}]` – jhnc Jul 26 '19 at 17:00
  • Only `{` needs escaping. `/\{}/`. But it ix more logical to use `indexOf`. – Wiktor Stribiżew Jul 26 '19 at 18:57

1 Answers1

1

The regex you look for is just \{\}

(which is written /\{\}/g as a JavaScript Regex literal with global flag).

You needed to escape the { and }, not to use a character set.

[{}] means either { or }.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758