6
I was taking a look at this @Sergio response here at stackoverlow:
There was an excerpt of code like this:
/^(\d)\1+$/.test(111)
What exactly does the \1 in that regular expression?
6
I was taking a look at this @Sergio response here at stackoverlow:
There was an excerpt of code like this:
/^(\d)\1+$/.test(111)
What exactly does the \1 in that regular expression?
8
If you have one or more capture groups in the regex \1 fetch the value captured in the first group. As a variable that takes the value of the group. If there is more than one you can use \2, etc..
For example:
/^(\w)\1(\d)\2$/accepts strings with two equal letters followed by two numbers equal to two.
For example aa11 or bb33 (example).
/^(\w)(\d)\1\2$/is similar to the example above but accepts interspersed patterns.
For example a1a1 or b3b3 (example).
3
It is the result of the first group that is representing the ( ), then number bar (\1) get its value. Other languages use dollar sign, e.g.: $1.
Regular expressions have utilities on the day, say you have a text file full of Inserts where dates are in format dd/mm/yyyy with a suitable editor it is possible to convert to yyyy-mm-dd replace.
23/10/2015
Utilize ([0-9]{2})\/([0-9]{2})/([0-9]{4}) to cover the date and \3\2\1 this will change the position of the year by the day, then just change the tab of \ for - and date will stay 2015-10-23.
The interesting thing is that usually the $1 is used "on the other side" of regexp. I mean, after capture. Equal preg_replace('/(\w+)/, 'Peguei isso $1', 'Aqui')
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.
are called
retrovisores, because they use something that has already been captured.– Guilherme Lautert
Just to finish solving the question: This only exists in javascript?
– Wallace Maxters
@Wallacemaxters this exists in almost all programming languages. Apparently RE2 does not support, but most support. Take a look here, in the column "Backreference".
– Sergio