Problem with Chrome's version causing regex malfunction

Seeking assistance with extracting a string from a URL of an image using regular expressions and the match() function in JavaScript. The code works flawlessly on Chrome version 62, but encounters issues on older versions like Chrome 58. Here are the specific lines of code causing the problem:

// cloneURL = "/assets/images/bonhommeneige.png"
var cloneName = cloneURL.match('(?<=images/)(.*?)(?=.png)');

While Chrome v. 62 successfully returns "bonhommeneige", Chrome v. 58 displays an error message stating "Invalid regular expression". In need of insights on what might be incorrect with the regular expression pattern.

Answer №1

Chrome version 58 does not support positive lookbehind. You should modify your regular expression to achieve the desired result without using lookbehind. One alternative is to use a non-capturing group like (?:images/):

var cloneURL = "/assets/images/bonhommeneige.png"
var cloneName = cloneURL.match('(?:images/)(.*?)(?=.png)');

console.log(cloneName);

Answer №2

If you need to extract the image file name, here is a useful method you can use:

// Method 1

let imgUrl  = "/assets/pictures/sunset.jpg";
let fileName = imgUrl.substring (
  imgUrl.lastIndexOf("/") + 1,
  imgUrl.lastIndexOf(".")
);

console.log(fileName);

// Method 2

let matches = imgUrl.match(/(?:pictures\/)(\S+)(?:\.jpg)/);

console.log(matches); // Retrieve the [1] element

Answer №3

To extract the value between the last forward slash / and the last period . (remembering to escape the period with \.) :

imageUrl = "/assets/photos/beach.jpg"

console.log( imageUrl.match(/[^/]+(?=\.)/)[0] )

console.log( imageUrl.match(/([^/]+)\./)[1] )

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

Tips for converting .WAV files to .MP3 format and streaming audio within a Meteor application

I have a function in my Meteor app that retrieves .wav data based on a text input. I now want to convert this .wav data into .mp3 format and play the audio within my app. How can I accomplish this? Meteor.call("watsonaudio",wusername,wpassword,text, funct ...

What is the best way to add a checkbox tag in Rails using JavaScript?

I am attempting to use javascript to append the values from option fields to checkboxes in Rails. Here is a snippet of my javascript code: $("#regions option").each(function(){ $("#region-checkboxes").append('<li><%= check_box_tag "region ...

Is it possible to transform a webpack configuration into a Next.js configuration?

i have come across a webpack configuration setup from https://github.com/shellscape/webpack-plugin-serve/blob/master/recipes/watch-static-content.md: const sane = require('sane'); const { WebpackPluginServe: Serve } = require('webpack-plugin ...

Tips for securely closing express servers to ensure that ports are properly released

One problem I have noticed, particularly on Mac computers, is that when I shut down a Node server using "CTRL + Z," the server stops running but the ports remain occupied. What is the correct method to close a server in order to release the ports and be a ...

Is there a way to choose multiple dropdown items that share the same header?

Utilizing the Fluent UI React Northstar Dropdown component in my React project, I've encountered an issue with multiple item selection. It seems that when there are several items sharing the same header value, only one can be selected at a time. What ...

The Power of Vue EventBus and Understanding the Lifecycle

How can I trigger an event in one component using an event from another? I have two child components at the same level, and when the updated() method is called in component1.vue, I want the setValues() method in component2.vue to be triggered. In componen ...

Isolating objects within a JSON array based on a single key

I am developing a system using Angular 6, and I need to create over 100 input fields. Since these inputs are used in multiple forms, I want to create a dynamic form. However, I'm trying to figure out the best way to connect the input configurations to ...

Enhancing Three.js interaction with the DOM (using linkify.js)

Having trouble making the click event work and linking a sphere with a URL using the latest version of three.js. Here is my code: // Code snippet will be here Any help or suggestions would be greatly appreciated. I'm still new to this! ...

Store in database and forward to a different web address

I have created an interactive quiz using jQuery. I need to add a specific feature at the end of the quiz. if (answered_questions === total_questions) { save the user's score to the database; redirect to specified URL; } The redirect_url is ...

`Using Twitter Bootstrap in mobile app development with javascript`

I have been utilizing Twitter Bootstrap 2.3 on one of my websites and I appreciate its responsiveness and use of media queries for mobile devices. However, I have noticed a lack of mobile-specific features, particularly linked listviews. In order to addres ...

RxJS Observables trigger the onCompleted function after completing a series of asynchronous actions

I have been attempting to design an observable that generates values from various asynchronous actions, particularly HTTP requests from a Jenkins server, which will notify a subscriber once all the actions are finished. However, it seems like there might b ...

Dynamically showcase dropdown menus within a single component

I am looking to implement multiple dropdowns in a single component, using one variable to control their display and also have them close when clicking away from the dropdown. Here is my code snippet: <div class="dropdown"> <button @cli ...

Prevent submission of form by disabling button until modifications are made to the original data in Vue.js

For experimental purposes, I have created a simple form and am trying to implement a feature where the button remains disabled until the original form data is changed. Additionally, I want to ensure that the button stays disabled even if the changed data i ...

Issues have been encountered with the correct functioning of reactive form validation in Angular 6

I am seeking assistance with Angular 6 reactive form validation. I have done some research on the official Angular website. My goal is to validate my form and display different error messages for different errors. This is a snippet of my component code: ...

Is it possible for one function to alter the local variable of another function?

After spending a frustrating hour trying to figure out why my JavaScript functions were not behaving as expected, I finally discovered the issue with some strategically placed alert() calls. It turns out that one function was meddling with the local variab ...

Retrieve the URL using a jQuery request in the P&G RFC

Hello, I am facing a problem with my AJAX URL requests. The issue is that when I include an RFC that contains the character "&" in it, only "P" is returned instead of the complete value. For example: Http://......../Get?RFCRec=P&G5609219R2 When the ...

Error 400: Invalid Request: Issue encountered when swapping code for Asana access token using Next.js

Encountered a 400 Bad Request error while trying to exchange the code for an access token at . I am unsure of the cause and would appreciate any assistance. Below is the code: const GetAsanaAccessToken = async (req, res) => { const body = { grant ...

Launching in an Angular reactive form

When working with Angular reactive forms, I am facing an issue where I want to set a form control value using a property from a service. However, upon submitting the form, the value of the form control does not reflect the expected property value from the ...

What is the best way to modify and execute js and css (less) files dynamically using ajax and jQuery?

Whenever I click a button, an ajax call is triggered to load various html code into a div with the id 'main'. While displaying the html content is not an issue, integrating and applying css and js code to my current page has proven to be challeng ...

What is the process for generating a comprehensive HTML table using AngularJS?

I have a set of data that needs to be dynamically loaded into an HTML page. Currently, I am using AngularJS to populate each table on the page. As of now, there are 8 tables in total. My goal is to make the table counts completely dynamic so that I can e ...