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

  • Home
  • SEARCH
  • 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 6709659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:54:29+00:00 2026-05-26T07:54:29+00:00

I am stuck with a little regular expression I am trying to match every

  • 0

I am stuck with a little regular expression
I am trying to match every substring that start with a given prefix using regular expression
in javascript

prefix = "pre-"
regex = /???/

"pre-foo-bar bar pre-bar barfoo".replace(regex, '')
// should output "bar barfoo"
  • 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-26T07:54:29+00:00Added an answer on May 26, 2026 at 7:54 am

    /\bpre-\S*\s?/g works, assuming you want to strip out the trailing space as well (per your example). If you want to leave it in, use /\bpre-\S*/g

    Correction

    \b only looks up word characters, and - is definitely not a word character. Unfortunately, JavaScript doesn’t support custom lookbehinds.

    /(\s|^)pre-\S*/g should work, but there will be a leading space compared to the example output given above. This checks for “pre-” preceded either by nothing or a space character and then followed by 0 or more non-space characters. It removes the whole block except the space. If the space is really important to you, you could do:

    str.replace(/(\s|^)pre-\S*\s?/g, function(wholeString, optionalSpaceCharacter) {
        return optionalSpaceCharacter;
    });
    

    Second Correction

    The complex replace I gave you won’t work if you have two in a row like, “pre-a pre-b pre-c”. You’ll end up with "pre-b " because of the \s? at the end. Your best bet to get the exact desired output is to use /(\s|^)pre-\S*/g and check the original string if it started with “pre-” if so, just remove one space from the beginning.

    str.replace(/(\s|^)pre-\S*/g, '').substring(str.substring(0, 4) == "pre-" ? 1 : 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression find and replace that I'm a little stuck on.
I'm trying to manufacture a regular expression that will capture a valid, arbitrary string
I'm a little stuck on a problem here. I'm trying to match multiple occurrences
A little stuck here. I have a simple question I guess. Given the following
I'm a little stuck trying to get my head around a django template. I
I'm a little stuck at the moment. I'm trying to figure out how to
Okay I'm a little stuck on trying to overload the << operator for my
I'm messing around with expression trees, but I'm little stuck. I have this expression:
I'm a little stuck on using Prawn/Prawnto to generate PDF documents. The Prawn documentation
I'm trying to learn MVC by building a full-featured website. I'm a little stuck

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.