Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:13:18+00:00 2026-05-31T06:13:18+00:00

I need to remove all words before the dash at the beginning of each

  • 0

I need to remove all words before the dash at the beginning of each sentence. Some sentences do not have words before dashes and dashes within the long sentence need to stay. Here is an example:

How do I change these strings:

PARIS — President Nicolas Sarkozy, running from behind for
reelection…

GAZA CITY —Cross-border fighting between Gaza and Israel…

CARURU, Colombia — Quite suddenly, the endless green of Amazonian
forest…

A year after an earthquake and tsunami devastated Japan’s northeastern
coast…

Into these strings:

President Nicolas Sarkozy, running from behind for
reelection…

Cross-border fighting between Gaza and Israel…

Quite suddenly, the endless green of Amazonian
forest…

A year after an earthquake and tsunami devastated Japan’s northeastern
coast…

How can I accomplish this with javascript (or php if javascript doesn’t allow it)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:13:19+00:00Added an answer on May 31, 2026 at 6:13 am

    This is a pretty straightforward regex problem, but geez, it’s not as straightforward as all the other answers assume. A few points:

    • Regex is the right choice – the split and substr answers won’t deal with the leading space, and can’t distinguish between a dateline with a dash at the beginning of a sentence, and a dash in the middle of your text content. Any option you use ought to be able to deal with content like: "President Nicolas Sarkozy — running from behind for reelection — came to Paris today..." as well as the options you suggest.

    • It’s tricky to automatically recognize that my test sentence above doesn’t have a dateline. Almost all the answers so far use the single description: any number of arbitrary characters, followed by a dash. That’s insufficient for a test sentence like the one above.

    • You’ll get better results by adding a few more rules, like fewer than X characters, located at the beginning of the string, followed by a dash, optionally followed by an arbitrary number of spaces, followed by a capital letter. Even this won’t work correctly with "President Sarkozy — Carla Bruni's husband...", but you’re going to have to assume that this edge case is sufficiently rare to ignore.

    All of which gives you a function like this:

    function removeDateline(str) {
        return str.replace(/^[^—]{3,75}—\s*(?=[A-Z])/, "");
    }
    

    Breaking it down:

    • ^ – must occur at the beginning of the string.
    • [^—]{3,75} – between 3 and 75 characters other than a dash
    • \s* – optional spaces
    • (?=[A-Z]) – lookahead – the next character must be a capital letter.

    Usage:

    var s = "PARIS — President Nicolas Sarkozy, running from behind for reelection...";
    removeDateline(s); // "President Nicolas Sarkozy — running from behind for reelection..."
    
    s = "PARIS — President Nicolas Sarkozy — running from behind for reelection...";
    removeDateline(s);  // "President Nicolas Sarkozy — running from behind for reelection..."
    
    s = "CARURU, Colombia — Quite suddenly, the endless green of Amazonian forest...";
    removeDateline(s); // "Quite suddenly, the endless green of Amazonian forest..."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem where I need to remove all code and triggers from
So I have these lines of javascript code that I need to remove all
I have a large list [[1,.., ..],[2,...,...],[5,...,...],[1,...,...]] I need to remove all elements that
I need to remove all classes of 'no-right-marg' on all #tips li elements. I
I need to remove all entries in a dictionary accordingly to a specified lower
In my application, _collection is a List from which I need to remove all
I'm adding dynamically labels to hbox, and i need to remove all spaces between
File: /home/USER/DIR/a http://www.here.is.a.hyper.link.net/ /home/USER/DIR/b http://www.here.is.another.hyper.link.net/ Need to remove all the odd lines in this
I need a way to remove ALL using statements from code and fully qualify
I need to remove the following script from my page(ASP.NET 3.5), leaving all other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.