-1

How to use variables or repeat through string in regular expressions?

/^(var1)bar(var1)$/.test('foobarfoo') //true
Andre
  • 1
  • 1

2 Answers2

0

You want something like this:

So if you wanted to replace "foobarbaz" into "bazbarfoo", you'd do this in sed like this (not sure what language you are using):

s/^(foo)(bar)(baz)$/\3\2\1/

You first capture your desired output with brackets () and then you can use the matches with backreferences like \1 \2 \3.

Franz Bettag
  • 437
  • 2
  • 8
0

Is that what you want?

console.log(/^([a-z]+)bar\1$/.test('foobarfoo'));
Toto
  • 89,455
  • 62
  • 89
  • 125