Regular expression locates strings that begin with

Looking for a way to use regex to transform the text from:

“isLarge: ‘aaaa bb-b ccc’”

to:

“'lg:aaaa lg:bb-b lg:ccc‘“

I have found a solution to replace all white spaces within quotes (https://regex101.com/r/ZrA1MP/1), but now I need this regular expression to only work when the string begins with "isLarge: "

Answer №1

Your previous regular expression had a flaw as it could detect a space in the given string

isLarge: aaaa bb-b ccc dd-d -ee',
. The updated regex that works is:

(?<=^isLarge: [^']*'[^']*) (?=[^']*'[^']*$)
  • (?<=^isLarge: [^']*'[^']*) Initially, it checks for a space preceded by isLarge: followed by any characters except a quote, then a quote and more characters not being a quote.
  • (?=[^']*'[^']*$) Next, it verifies if the space is succeeded by a quote.

Edit

reg = /(?<=^isLarge: [^']*'[^']*) (?=[^']*'[^']*$)/gm;
str = "isLarge: 'aaaa bb-b ccc dd-d -ee',\nisLarge: aaaa bb-b ccc dd-d -ee',\nisMedium: 'aaaa bb-b ccc dd-d -ee'";
r.test(str)
// true
str.replaceAll(r, " lg:").replaceAll("isLarge: ",'');
// "'aaaa lg:bb-b lg:ccc lg:dd-d lg:-ee',
// aaaa bb-b ccc dd-d -ee',
// isMedium: 'aaaa bb-b ccc dd-d -ee'"

If the pattern is ineffective, it may be due to the regex engine's lack of support for lookbehind with non-fixed width. In such cases, capturing [^']*'[^']* and reusing it in the replacement scheme can resolve the issue, like this:

(?<=^isLarge:)([^']*'[^']*) (?=[^']*'[^']*$)

Then utilize $1 in the replace string to retain it, for instance: "$1 lg:"

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

Developing a Secondary User within Meteor.JS after Establishing the Primary User

Is it possible to automatically create a secondary user upon registration of the primary user using a form generated with the useraccounts:core package? An issue arises when attempting to run Accounts.createUser within Accounts.onCreateUser, resulting in ...

How can I retrieve Feature Information exclusively from the currently visible WMS layers in OpenLayers 3?

I am working with an Openlayers map that features multiple WMS layers. I am trying to use the "getGetFeatureInfoUrl" function to request feature information, but only for the layers that are currently visible on the map. Additionally, if there are multiple ...

Is it possible to dynamically assign trigger and target divs to bPopup?

On our ColdFusion page, we have a dynamic list of paired divs created using a loop over a query. For example, each pair consists of internshipHandleX and internshipHiddenX: <div id="internshipHidden#internship.currentrow#" class="hidden pop-up"> We ...

cannot wait for promise in loop to avoid delaying the request

In the API endpoint of a Next.js webapp, this code is designed to fetch all the GitHub repositories, including their names and number of contributors. However, an issue arises when using Promise.all - the call does not return anything (resulting in a stall ...

Combine elements of an array of objects to create a coherent sentence

Received an API response structured as follows: "payload": { "paragraph": { "id": 2692, "words": [ { "id": 21679, "position": 2, ...

Transferring data between Jade templates

In the process of building a compact CMS system prior to diving into Node.js websites using Express, Jade, and Bootstrap, I encountered a minor setback. To enhance modularity, I am employing includes for various components like the navigation header on th ...

Show Zeroes in Front of Input Numbers

I am working with two input fields that represent hours and minutes separately. <input type="number" min="0" max="24" step="1" value="00" class="hours"> <input type="number" min="0" max="0.60" step="0.01" value="00" class="minutes"> This se ...

Transforming JSON into Excel format with Azure Function and storing it in blob storage

Is there a way to store the Excel file generated by this JavaScript code in a blob storage? I'm looking for a solution similar to fs.safeFileSync. Any ideas on how to achieve this? const fs = require('fs'); var json2xls = require('json2 ...

What is the best way to retrieve and parse XML using node.js?

Is there a way to fetch an XML file from the internet using node.js, and then parse it into a JavaScript object? I've looked through the npm registry but can only find examples on parsing XML strings, not fetching them. ...

Using the Jquery load function to add new content to existing HTML

I'm attempting to create an AJAX request that will insert additional HTML content into the existing content within specified tags. Below is the structure of my load function. <script> $(document).ready(function(){ $(".tid-select").cha ...

Issues with router middleware functionality in Node.js hinder proper operations

Currently, I am working with Nodejs and utilizing "Express js". One of the tasks at hand involves implementing a "Router Middleware" function. With my current code setup, whenever I access "http://localhost:3000", the "router middle" functionality is tri ...

Determine the element's classification for the word "this"

When using the following code: <a href="http://www.google.com" onclick="n();"> I am trying to retrieve the element type and, if it's an img, get the value of src. However, if it's an a, I want to get the value of href. I attempted the fo ...

Use regular expressions to extract information enclosed within quotation marks

Here is the string we have: feature name="osp" We want to extract specific parts of this string and create a new string. The word "feature" may vary, as well as the content inside the quotes, so our solution needs to be flexible enough to capture any var ...

Discover the best methods for accessing all pages on a Facebook account

I attempted to retrieve a list of all Facebook pages, but encountered an error. The error message states: 'request is not defined.' Here is the code snippet: var url = 'https://graph.facebook.com/me/accounts'; var accessToken = req.u ...

The length of the string indicates it has a length of 7 characters, however, in reality, the actual length of the string is

My declaration is constantly like this: const table = req.params.table; When I send a request with the parameter "circle" as the table, table.length returns 7. I attempted to modify it like so: table.toString().trim().length However, it still displays ...

The body classList variable is inaccurately updated when making a JQuery Ajax call

Currently, I am in the process of developing a script to manage Ajax page transitions using JQuery's Ajax request function. Within the success callback of the Ajax function, it is essential for me to access the classList of the current page's bod ...

Implementing Observable in a function is a simple and effective way to

According to luwojtaszek answer in this topic: How to Export JSON to CSV or Excel - Angular 2 I tried out the following code: public exportAsExcelFile(json: any[], excelFileName: string): void { const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_s ...

Themeing for dark mode using styled components in Next JS

I have been searching for an answer to this question, but haven't found one yet Currently, I am using styled components with next js along with the use-dark-mode hook to manage theme changes and detection The global styles switch seems to work fine ...

Visuals and PDF Generation Tool

Trying to generate project report pdf's using pdfmake has presented a challenge when it comes to displaying images. A function I have for creating a pdfmake "object" looks like this: function singleProject(data) { return { text: "Project ...

Tips for efficiently inserting large amounts of JSON data using node.js

My goal is to insert this JSON data into my SQL Server database. The JSON consists of bulk data with a detailed structure. Below is an example of the JSON data I aim to insert: [ { No_BPS:'BSWEB12345', Kd_Plg:'MMIM026', ...