Discovering the regex pattern to locate any existing code and substitute it with nothing

How do I remove possible code from strings and replace it with blank?

For example:

Input =

9eac83a4-80f4-4a2e-b0fe-7a4a9329ff62Manual Handling.pdf

Desired Output = Manual Handling.pdf

Input =

14a19827-8f33-4666-a3cc-ea257875f1f7Fire&Evac.pdf

Desired Output = Fire&Evac.pdf

Input =

67a89d74-57a9-43cc-9576-001f9315d292BLS-1.pdf

Desired Output = BLS-1.pdf

Input =

7622a004-43b8-4357-8c95-c8a269e6ef7827276e859ef10d-Mango Super.pdf

Desired Output = Mango Super.pdf

Input = d5b0f745aa80d9- Calc.png

Desired Output = Calc.png

Input =

d5b0f745aa80d9980d090- Covid-19-Test.png

Desired Output = Covid-19-Test.png

Input =

d5b0f745aa80d9980d090-2a004-43b8-4357-8 Covid-19 Vacc.png

Desired Output = Covid-19 Vacc.png

Can someone help me achieve this?

I've tried this regex but it's not working for all the cases mentioned above:


    var str = "9eac83a4-80f4-4a2e-b0fe-7a4a9329ff62Manual Handling.pdf";
    var cleanStr = str.replace(/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/g, "");
  

Any assistance would be greatly appreciated. Thank you!

Answer №1

Here is a possible solution:

.*?(?=[A-Z].*$)
  • .*? captures everything in a loose manner
  • (?=[A-Z].*$) - checks for a capital letter followed by anything ahead of it; this part is not captured

const data = `9eac83a4-80f4-4a2e-b0fe-7a4a9329ff62Manual Handling.pdf
14a19827-8f33-4666-a3cc-ea257875f1f7Fire&Evac.pdf
67a89d74-57a9-43cc-9576-001f9315d292BLS-1.pdf
7622a004-43b8-4357-8c95-c8a269e6ef7827276e859ef10d-Mango Super.pdf
d5b0f745aa80d9- Calc.png
d5b0f745aa80d9980d090- Covid-19-Test.png
d5b0f745aa80d9980d090-2a004-43b8-4357-8 Covid-19 Vacc.png`;

const pattern = /.*?(?=[A-Z].*$)/;
data.split(/\n/).forEach(e => console.log(e.replace(pattern, "")));

Answer №2

For a solution that covers all scenarios, the straightforward pattern /[a-f\d-]{14,} */ can be utilized:

const data = `9eac83a4-80f4-4a2e-b0fe-7a4a9329ff62Manual Handling.pdf
14a19827-8f33-4666-a3cc-ea257875f1f7Fire&Evac.pdf
67a89d74-57a9-43cc-9576-001f9315d292BLS-1.pdf
7622a004-43b8-4357-8c95-c8a269e6ef7827276e859ef10d-Mango Super.pdf
d5b0f745aa80d9- Calc.png
d5b0f745aa80d9980d090- Covid-19-Test.png
d5b0f745aa80d9980d090-2a004-43b8-4357-8 Covid-19 Vacc.png`;

const pattern = /[a-f\d-]{14,} */;
data.split(/\n/).forEach(e => console.log(e.replace(pattern, "")));

If this regex pattern is generating incorrect matches with other input strings, it may be beneficial to create a more precise definition for the prefix id format(s).

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

How can I retrieve the parent scope in an Angular UI nested named view?

One of the challenges I faced in my application was dealing with nested views within several screens. After experimenting with different approaches, I discovered that creating modal dialog boxes and sliding panels as actual nested states with corresponding ...

How can I update the value of a span element that was added to the HTML document through an AJAX request?

When a modal is triggered on click, data is added to it through an AJAX request to a PHP file. After the AJAX call, I want the values in span elements within the modal to change simultaneously with the input field of the modal. I attempted this JavaScript ...

How can I use Jquery to loop through each combobox on the page and delete those with a specific class name?

I am currently dealing with a page that contains multiple combo-boxes. Each combo-box has a default option set as empty, which is given the class name 'hide'. This functionality seems to work perfectly in Chrome and Firefox, as the hidden options ...

In my sequence of Promises, a "reject is not defined" error led to the rejection of a Promise

In my code, I set up a chain of Promises like this: let promise = new Promise((resolve, reject) => { imgToDisplay.onload = () => { resolve(imgToDisplay.width); } }) .then((result) => { window.URL.revokeObjectURL(imgToD ...

What is the best way to access a variable within the .each() function in jQuery?

One common dilemma often faced is figuring out how to ensure that markup remains accessible within the scope of this .each() function. Instead of focusing solely on resolving this specific issue, my interest lies more in discovering a method to access ext ...

AngularJS mdDialog not supporting Tinymce functionality

I'm attempting to integrate the TinyMCE editor into an AngularJS mdDialog. Working Plunker: http://embed.plnkr.co/s3NsemdcDAtG7AoQRvLh/ Plunker with issues: http://embed.plnkr.co/fL8kGLl3b4TNdxW1AtKG/ All features are working fine except for the d ...

Storing Firebase credentials securely in Vue.js applications after deployment with environment variables (env_var)

I've been attempting to deploy my firebase&vue application, but I've encountered issues with adding firebase credentials to the environment variables. Here's the structure within vue.js: config ---- key.js ---- keys_dev.js ---- key ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

What could be causing the issue with HTML not being printed upon button click in ReactJS?

My goal is to display the word "Hello" on the screen when the add button is clicked. However, I am encountering an issue where it is not showing up. Any insights or solutions would be greatly appreciated! import React, { Component } from 'react'; ...

Encountering problems when trying to establish a connection to my MySQL database while utilizing a REST API server built

As a newcomer to coding servers and JavaScript, I am currently taking steps to establish a REST API server and connect it with my SQL database locally. My setup involves running Ubuntu 18.04 with NODE js. So far, I have managed to successfully create a RES ...

In Vue3, have you ever wondered why the $emit function seems to work fine before a promise fetch,

https://i.sstatic.net/yJmDY.jpg I have encountered an issue while attempting to pass the result of a promise fetch from a child component to a parent component using emit. Strangely, the emit function was working perfectly fine before the $fetch operation, ...

The received URL from the POST request in React is now being opened

After completing an API call, I successfully received the correct response in my console. Is there a way to redirect my React app from the local host to the URL provided (in this case, the one labeled GatewayUrl under data)? Any assistance would be greatly ...

Utilizing ion-slide-box within an ion-content container that allows for scrolling

I've created an Ionic view with the following structure: <ion-content scroll="true"> <ion-list> ... some ion items... <ion-item> <ion-slide-box> <ion-slide ng-repeat="image i ...

The getStaticProps function in Next.js does not pass any data back to the pages

After setting up my hosted database, I encountered an issue with fetching data from it. Despite confirming that the database is running smoothly through the Swagger app, no data appears when called via API form. import React from 'react'; export ...

Is there a way to retrieve the text of a clicked button with execute_script?

Is there a way to extract text from a clicked button and send it back to Python? The user clicks the button in the Selenium WebDriver browser using the mouse. This is what I attempted: x=driver.execute_script("$(document).click(function(event){var te ...

When the user clicks, I plan to switch the audio source

I am looking to update the audio source when a button is clicked, but I am having trouble getting it to work. image description data() { return { audioSrc: '' } }, methods: { setActiveAudio(item) { this.$refs.audioE ...

Display a div beside it when hovering over a specific part of the image

If I have an image that is 200px wide and 900px tall How can I make a div display alongside it when hovering over a specific section of the image? ...

Automatically select and pre-fill a list item based on its value using jQuery

I'm a beginner in JQuery. In my application I have the following: Here is my HTML code: <ul id="list"> <li> <a class="selected" value="0" href="#" id="sel">ALL</a> </li> <li> <a hre ...

Why isn't my JavaScript Alert displaying a string message?

I'm feeling a bit puzzled and I have a feeling there must be an easy solution, so please lend me a hand. Here's the code snippet that is causing me some confusion: $new = "1"; <script language="javascript"> alert(<?php echo $new; ...

What steps should I follow to enable webview autocomplete for a login form on Ionic's Capacitor in iOS?

I am trying to figure out how to activate the autocomplete feature for a login form in Capacitor when using Ionic React. The issue arises when bundling the web app in Capacitor, as the autocomplete functionality seems to disappear. Although it works on Saf ...