utilizing regular expressions in TypeScript

I'm currently working with Angular 2 and I have a URL link: www.abcd.com/Computers_Accessories/panache-air-pc/P-coac-20620024815-cat-z.html#newId=P-coac-41130779424?trackId=paym&subTrackId=&infitag=1234

My goal is to remove the portion #newId=P-coac-41130779424 from the URL and append it to the end, resulting in the new URL: www.abcd.com/Computers_Accessories/panache-air-pc/P-coac-20620024815-cat-z.html?trackId=paym&subTrackId=&infitag=1234#newId=P-coac-41130779424. I'm wondering if there is a way to use Regex in TypeScript to achieve this or if there is another method that would be more appropriate.

Answer №1

To implement this functionality, you need to utilize the following regular expression:

(.*)(#newId=.*)(?=\?)(.*)

Once you have the regex pattern, replace it with the content captured by the 1st group and then the 3rd group followed by the 2nd group.

For a live demonstration, please refer to the demo.

let str = 'www.abcd.com/Computers_Accessories/panache-air-pc/P-coac-20620024815-cat-z.html#newId=P-coac-41130779424?trackId=paym&subTrackId=&infitag=1234';

str = str.replace(/(.*)(#newId=.*)(?=\?)(.*)/g, "$1$3$2");

console.log(str);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to display API error messages to the user?

When a user tries to upload a file that is not an image, I need to display the error message returned from a REST API. The JSON response received from the API will look something like this: { "publicError": "Could not be uploaded, it is not an image! ...

New data field is created with AngularFire2 update instead of updating existing field

I am facing an issue with updating a Firestore model in Angular 6. The model consists of a profile name and a list of hashtags. The "name" is stored as the value of a document field, while the "hashtags" are stored as keys in an object. However, every time ...

What are the best practices for implementing jquery owlCarousel within an Angular 4 component?

I've included my 'carousel.js' file like this: $('#owl-carousel-1').owlCarousel({...}); and in carousel.component.html, I have: <div id="owl-carousel-1" class="owl-carousel owl-theme center-owl-nav home- carousel">....< ...

Enable Parse5's case sensitivity

Recently, I've attempted to parse Angular Templates into AST using the powerful parse5 library. It seemed like the perfect solution, until I encountered an issue - while parsing HTML, it appears that the library transforms everything to lowercase. Fo ...

Using TypeScript to validate the API response against specific types

I'm intrigued by the scenario where you expect a specific data type as a response from fetch / Axios / etc, but receive a different type instead. Is there a way to identify this discrepancy? interface HttpResponse<T> extends Response { parsed ...

Is it possible to remove generic T in typescript if the mysql result value is an array but generic T is not an array?

type User = { name: string email: string } This is the code snippet, import type { PoolConnection, RowDataPacket, OkPacket } from "mysql2/promise"; type dbDefaults = RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[]; type dbQuery& ...

Improving Angular 2 Charts with Multiple Chart Updates

Looking for some help with my code. I am fetching server data using an API for monitoring purposes and have multiple charts set up. The issue is that only the first chart updates every second when I call the API, but the others do not update. Any suggestio ...

typescript: How to restrict an array's type in a specific order

Is there a way to restrict the types of elements in an array in TypeScript without specifying paradigms? For example, instead of defining arrays as follows: const arr:Array<any> = [] I would like to be able to specify a specific order for the arr ...

Top Tip for Preventing Angular Version Compatibility Issues

Here is an illustration that delves into my inquiry ----> Version Conflict The dilemma arises when my product has a dependency on a node package, which in turn relies on a specific version of Angular, denoted as version #y. However, my product itself ...

Changing the title of your document and app bar in React Router when navigating pages

I'm looking into implementing the react router in a TypeScript project. How can I dynamically update the document title (document.title) and the app bar title (<span className="appbartitle">Home</span>) in a single page application based o ...

The Gatsby Head API is experiencing issues with correctly formatting scripts

I'm currently exploring the use of Gatsby's Head API with Gatsby.js (4.24.2) and TypeScript, and I am encountering some inconsistent outcomes. Here is the code I am working with, it is functional but certain scripts are failing to compile: const ...

Generating dynamic forms using JSON Schema in Angular 8 can greatly streamline the form creation process

Struggling with generating dynamic forms from JSON Schema in Angular 8, I stumbled upon a couple of libraries. One seemed outdated with its last commit around 2 years ago, while the other appeared to be a more recent fork of it for Angular 8. The first li ...

I'm having trouble retrieving my variable within the socketcluster's socket.on function

How can I store the value of msg in the variable sample when sample is not accessible inside the callback function? import { Injectable } from '@angular/core'; import * as socketCluster from 'socketcluster-client'; @Injectable({ pro ...

Encountering an issue in Angular where data.slice is not functioning properly, resorting to using parseInt to convert strings into Date

Looking to convert the data retrieved from the database into numbers and dates with ease. One set of data is in milliseconds while the other is in timestamps. https://i.stack.imgur.com/HdM0D.png The goal is to transform both types into numbers first, the ...

Insert HTML elements into the variable that holds data retrieved from firestore

Recently, I received a Firestore response in the following format: https://i.sstatic.net/49dX9.png Within my TypeScript variable {{task.title}}, I have access to this data on my component. My goal is to incorporate a hyperlink specifically on the person& ...

Angular issue: Readonly or disabled input fields not submitting data

Struggling with creating a form in Angular 2 to submit dynamically generated values from a service. The goal is to calculate the equivalent amount of bitcoin for X chilean pesos using Angular's http module to fetch the price. However, facing an issue ...

What is the best way to retrieve the selected values from a checkbox using Angular?

I am working on creating a dynamic checkbox from a list of cartoon data. Each checkbox represents a cartoon, and when selected, I need to be able to read the values in a TypeScript function. In my HTML file <div *ngFor="let cartoon of cartoonsDa ...

Experiencing difficulties with ngModel binding and ngForm imports despite the seemingly correct code. Encountering several strange issues, could this be due to compatibility issues with Angular

ERROR in src/app/Register/register.component.html:12:9 - error NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'. 12 [(ngModel)]="model.UserName" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src ...

Encountered an npm error while attempting to start an Angular project

When attempting to run my project using ng serve -o, a warning message popped up on the screen stating: Your global Angular CLI version (15.2.2) is higher than your local version (15.0.4). The local Angular CLI version will be used. To disable this warnin ...

The outcome of the .then function is not defined following another .then function

I am struggling with handling async requests in Angular within Ionic4. I have successfully loaded a .json file and saved it into an array, but when trying to edit the array within a loop, my promise resolves before the loop finishes. As a newcomer to Angul ...