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 6967061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:16:21+00:00 2026-05-27T16:16:21+00:00

Probably a simple one but my knowledge of creating regular expressions is a little

  • 0

Probably a simple one but my knowledge of creating regular expressions is a little vague.

I’m trying to match any string followed by a comma and a space except if it is ‘Bair Hugger’ or ‘Fluid Warmer’

Here is what I have so far

var re_comma = new RegExp("\w+[^Bair Hugger|Fluid Warmer]" + ", ", "i");

Any ideas?

  • 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-27T16:16:22+00:00Added an answer on May 27, 2026 at 4:16 pm

    New answer

    Regarding your example I’d say it is really easier to split the string and iterate over it:

    function filter(str, delim, test) {
        var parts = str.split(delim),
            result = [];
    
        for(var i = 0, len = parts.length; i < len; i++) {
            if(test(parts[i])) result.push(parts[i]);
        }
    
        return result.join(delim);
    }
    
    str = filter(str, ', ', function(s) {
        s = s.toLowerCase();
        return s === 'bair hugger' || s === 'fluid warmer';
    });
    

    Otherwise, your expression becomes something like this:

    new RegExp("(^|, )(?!(?:Bair Hugger|Fluid Warmer)(?:$|, )).+?(, |$)", "i");
    

    and you have to use a callback for the replacement to decide whether to remove the preceding , or trailing , or not:

    str = str.replace(re_comma, function(str, pre, tail) {
        return pre && tail ? tail : '';// middle of the string, leave one
    });
    

    The intention of this code is less clear. Maybe there is a simpler expression, but I think filtering the array is still cleaner.


    Old answer: (doesn’t solve the problem at hand but provides information regarding regular expressions).

    [] denotes a character class and will only match one character out of the ones you provided. [^Bair Hugger|Fluid Warmer] is the same as [^Bair Huge|FldWm].

    You could use a negative lookahead:

    new RegExp("^(?!(Bair Hugger|Fluid Warmer), ).+?, $", "i");
    

    Note that you have to use \\ inside a string to produce one \. Otherwise, "\w" becomes w and is not a special character sequence anymore.You also have to anchor the expression.

    Update: As you mentioned you want to match any string before the comma, I decided to use . instead of \w, to match any character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably quite a simple one, but I'm not having any luck in the docs
This is probably a simple one but I can't seem to figure it out.
This is probably a simple one to answer, but I'm stuck, so here goes.
This is probably a really simple question but one I've never quite worked out
So this one is probably very simple, but I'm having a bit of trouble
This is probably a really simple one but I couldn't find the answer. I
Probably a simple one, but I need some help. I had some radio buttons
This is probably a simple one but I cant get my head around it.
I am trying to do something I know is probably simple, but I am
Another annoying one for me but probably something simple. I have a number of

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.