Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Regex - Get Started
  • Regex Syntax
  • Characters
  • Metacharacters
  • Quantifiers
  • Character Classes
  • Grouping
  • Lookarounds
  • Substitution
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Substitution in Regex

Substitution in regex refers to the process of replacing matches of a pattern with a specified replacement string. It allows you to transform or modify text based on the matches found in the input string.

SubstitutionDescription
$ numberIncludes the last substring matched by the capturing group that is identified by number, where number is a decimal value, in the replacement string
${name}Includes the last substring matched by the named group that is designated by (?<name> ) in the replacement string.
$$Includes a single "$" literal in the replacement string.
$&Includes a copy of the entire match in the replacement string.
$`Includes all the text of the input string before the match in the replacement string.
$'Includes all the text of the input string after the match in the replacement string.
$+Includes the last group captured in the replacement string.
$_Includes the entire input string in the replacement string.

You can replace a string using regex backreference using a string replacement function/method provided by the programming language or tool you are using.

The following example demonstrate how you can replace a string using regex backreference in JavaScript:

Example: Regex Substitution in JavaScript
var regexPattern = /(Hello)/; var str = 'Hello World!'; //replaces Hello with Hi var newstr = str.replace(regexPattern, 'Hi'); console.log(newstr);  //output: 'Hi World!'
@ttHelpers.TryItLink("cid=javascript-3z9dyttpd")

Replace String using Backreference

You can replace string by taking reference of the matched group using $. Use the respective matched group number e.g. $1 for first group, $2 for second and so on.

The following uses backreference $1, $2, $3, and $4 for the captured groups and add - between each matched group.

Example: Regex Substitution in JavaScript
//captures first digit as group#1, second three digits as group#2 //and four digits as group#3 and last four digits as group#4 var regexPattern = /(\d)(\d3)(\d3)(\d4)/; var str = '+12015645555'; //adding - between each group var newstr = str.replace(regexPattern, '$1-$2-$3-$4'); console.log(newstr);//output: +1-201-564-5555
@ttHelpers.TryItLink("cid=javascript-3z9dz3mjj")

In the regex pattern, (\d) finds one digit as a group, (\d3) as a second group, (\d3) as a third group, and (\d4) as a fourth group which can be referenced using $1, $2, $3 and $4 respectively.

Note: There might be slight variations in implementing substitutions based on the programming language you are using.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.