Is there a way to retrieve the final word following a unique character?

Currently, I have this:

 \- (.*)

When analyzing the text below:

September 2014 - Media

I am trying to extract "Media", but instead I am retrieving "- Media"

Answer №1

Your regular expression is accurate, just make sure to print the group index 1 instead of group 0 and there's no need to escape the "-" symbol.

Check out the DEMO

Observe the extracted string Media on the right side in the demo link provided above.

OR

To only match Media, use the following regex:

- \K.*

DEMO

If your programming language does not support \K, you can utilize a lookbehind like this:

(?<=- ).*

DEMO

> "September 2014 - Media".match(/[^- ]+$/g)
[ 'Media' ]
> var r = "September 2014 - Media";
undefined
> console.log(/- (.*)/.exec(r)[1])
Media

Answer №2

In cases where your programming language supports lookbehind, you can try using the following regex pattern:

(?<=-)(.*)

If lookbehind is not supported in your language, you can use the following alternative pattern:

([^-]*)$

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 retrieving multiple data outputs from an ajax success function

Within my project, I have two separate JavaScript files named myJs1.js and myJs2.js. One of the methods in myJs1.js is invoking a method from myJs2.js. My goal is to retrieve the values r1 and r2 into the results (within myJs1.js). I attempted to achiev ...

Upon clicking a table row, generate a div underneath containing mapped data

My dynamic table is currently being filled with rows based on an array, but I want to display more data in an expanded view when a button in a row is clicked. However, I'm facing challenges as it seems I can't have two table rows within the same ...

Communicating with Controllers: Troubleshooting Module Issues in Angular JS

Currently, I am in the process of learning Angular through this informative video: http://www.youtube.com/watch?v=LJmZaxuxlRc&feature=share&list=PLP6DbQBkn9ymGQh2qpk9ImLHdSH5T7yw7 The main objective of the tutorial is to create a rollover effect ...

Why isn't the function in my React child component passing its parameters to the parent component's function as expected?

In the parent: const [currentPinPosition, setCurrentPinPosition] = React.useState({ lat: 0 , lng: 0 }); const updateCurrentPinPos = (position) => { console.log(position); setCurrentPinPosition({ lat: position.lat, lng: position.lng }); }; / ...

"Experience the power of Angular.js through seamless animations created by blending the capabilities

I want to smoothly fade out the old view and fade in the new view. The challenge is that the new content must be positioned absolutely until the old one fades out. I also want to set a specific top position and height for the new content, but when I try to ...

Combining an Angular factory with another factory

I'm facing some difficulties with injecting one factory into another. The error message I'm getting is: `ReferenceError: testDataService is not defined` I thought it would be a simple issue to resolve, but it's turning out to be quite c ...

Is it possible to create a Nuxt.js static website that incorporates a dynamic single-page application built with Vue.js

Currently researching Static Site Generators for a client project, I discovered Vue.js and Nuxt.js, which appear promising. Given the choice between Vue.js and React, I lean towards Vue at the moment. The client's website consists of 120 pages with s ...

Best practices for handling AJAX GET and POST requests in EmberJS

EmberJS has captured my interest and I am thoroughly enjoying diving into it. Although there is a learning curve, I truly believe in the meaningful principles it stands for. I am curious about the process of making GET and POST calls in Ember JS. While I ...

Disabling the InfiniteScroll feature when navigating to a new page via AJAX

We are currently utilizing the jQuery Infinite Scroll Plugin on our website. Despite the fact that the plugin is no longer being maintained, it perfectly serves our needs. However, the challenge we are facing is related to our ajax-based site. Whenever the ...

Manipulating an Array of Objects based on conditions in Angular 8

I have received an array of objects from an API response and I need to create a function that manipulates the data by enabling or disabling a flag based on certain conditions. API Response const data = [ { "subfamily": "Hair ...

Is it possible to utilize AngularJS's $q functionality outside of an Angular component?

I am currently working on a browser application, where I have a specific file responsible for creating and initializing an object. This file is written in plain Javascript rather than being part of the Angular ecosystem like the rest of the app, which is b ...

Can Angular 4 experience race conditions?

Here is a snippet of my Angular 4 Service code: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Perform calculations and add results to myAr ...

Finding and implementing the page URL in JavaScript

Basically, I only want a certain script to be called if the URL is not "blogs.html". Here are some examples where the script should NOT be called: mydomains.com/blogs mydomains.com/blogs.html And here are some examples where the script should be called: ...

Autocomplete feature enhanced with the ability to validate and clear out

Here's a snippet of my HTML code: <div class="form-group"> <label for="date_chargement" class="col-sm-4 control-label">Minimum loading date:</label> <div class="col-sm-2"> <input type="text" class="form-control te ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

Using React js to transform objects into arrays for useState

Hey there! I'm currently working on a webshop demo project for my portfolio. I've encountered an issue while trying to fetch data from commerce.js. The data is in the form of objects, which are causing problems when I try to map them. I've a ...

Achieve Vue to refresh the view or component

I am facing a dilemma while working on a specific component. The component in question is called "RecentUpdates". Within this component, I am passing props down to several other components, as indicated at the beginning of the file. My issue arises when ...

Using javascript and d3.js to display a set number of items on each line through printing

I'm currently working on a project that involves printing a specific number of rectangles per line using d3.js var rectangle = svgContainer.selectAll("rect").data(data).enter().append("rect") .attr("x", function(d,i){ return i*5}) ...

Is the 404 page being utilized as a fallback for AJAX requests?

I am looking to create a one-page website that utilizes history.pushstate to modify the URL. My plan involves having only one HTML file for the site, which would be the 404 error page. This setup would redirect users to that page regardless of the URL they ...

Conflicting issues with jQuery's load() method

I am facing an issue with loading two HTML pages into a single div. I have index.html and try.html in the root folder, where try.html is loaded into index.html's div (#content) when Button 01 is clicked on index.html. The problem arises when I further ...