Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
check-for-all-or-none
check-for-mixed-grouping-of-characters
extract-matches
find-characters-with-lazy-matching
find-more-than-the-first-match
find-one-or-more-criminals-in-a-hunt
ignore-case-while-matching
match-a-literal-string-with-different-possibilities
match-all-letters-and-numbers
match-all-non-numbers
match-all-numbers
match-anything-with-wildcard-period
match-beginning-string-patterns
match-characters-that-occur-one-or-more-times
match-characters-that-occur-zero-or-more-times
match-ending-string-patterns
match-everything-but-letters-and-numbers
match-letters-of-the-alphabet
match-literal-strings
match-non-whitespace-characters
match-numbers-and-letters-of-the-alphabet
match-single-character-with-multiple-possibilities
match-single-characters-not-specified
match-whitespace
positive-and-negative-lookahead
remove-whitespace-from-start-and-end
restrict-possible-usernames
reuse-patterns-using-capture-groups
index.md
specify-exact-number-of-matches
specify-only-the-lower-number-of-matches
specify-upper-and-lower-number-of-matches
use-capture-groups-to-search-and-replace
using-the-test-method
index.md
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
dns
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
graphql
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mariadb
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
powershell
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env

52 lines
1.3 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Reuse Patterns Using Capture Groups
---
## Reuse Patterns Using Capture Group
## Hint 1:
Given code below:
```javascript
let testString = "test test test";
2018-10-12 15:37:13 -04:00
let reRegex =/(test)\s\1/;
let result = reRegex.test(testString);
```
`result` will match only `test test` because `\1` in this example stands for the same text as most recently matched by the 1st capturing group `(test)`.
If we were to literally translate the regex, it would look something like this:
2018-10-12 15:37:13 -04:00
```js
let re = /(test)\s\1/;
let literalRe = /test\stest/;
2018-10-12 15:37:13 -04:00
```
Both `re` and `literalRe` would match the same thing.
2018-10-12 15:37:13 -04:00
## Hint 2:
Given the code below:
```javascript
let testString = "test test test";
2018-10-12 15:37:13 -04:00
let reRegex =/(test)(\s)\1\2\1/;
let result = reRegex.test(testString);
```
`result` will match whole `test test test` because:
2018-10-12 15:37:13 -04:00
`\1` repeats (test)
`\2` repeats (\s)
## Hint 3:
The code below:
```javascript
let testString = "test test test test test test";
let reRegex =/(test)(\s)\1\2\1/g;
let result = reRegex.test(testString);
```
because we used `\g`, our Regex doesn't return after first full match (`test test test`) and matched all repetitions.
## Spoiler Alert - Solution Ahead!
## Solution:
```javascript
let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);
```