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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:36:10+00:00 2026-06-09T12:36:10+00:00

How do I write a Javascript regular expression that matches everything except a given

  • 0

How do I write a Javascript regular expression that matches everything except a given string (“ABCD”)?

Something like /[^ABCD]/ except I don’t want to match everything that isn’t the letter A, B, C or D. I want to match everything that isn’t the string “ABCD”.

Basically I want this to happen:

var myStr = "ABCA ABCB ABCD BCD ABC"
myStr.replace(/!(ABCD)/g,'') // returns ABCD
  • 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-06-09T12:36:12+00:00Added an answer on June 9, 2026 at 12:36 pm

    Okay, I had misinterpreted the question. It seems you want to test for the presence of ABCD, and if you find it, replace the whole string with just that: ABCD. This will do that:

    s = s.replace(/.*(ABCD).*/, '$1');
    

    But it will leave the string unchanged if there’s no ABCD in it. If you want to delete the string in that case, you have to make the capture optional. But then you have to alter the first part of the regex to make it “sneak up” on the capture:

    s = s.replace(/^(?:(?!ABCD).)*((?:ABCD)?).*$/, '$1');
    

    That forces it to try to capture ABCD at every position. (It also slows things down massively–not an issue in this case, but something to keep in mind if you use this technique on large inputs.)

    So a pure-regex solution does exist, but I like @bažmegakapa’s solution better. 😀


    original answer:

    /^(?!ABCD$).*/
    

    Note that this will also match an empty string. If you have any positive requirements, you can change the .* to whatever you need. For example, to match one or more uppercase ASCII letters but not the exact string ABCD, you can use:

    /^(?!ABCD$)[A-Z]+$/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a regular expression in javascript that only allows users to
How to write the regular expression for the below string using Javascript. [Account].&[1]+[Account].&[2]+[Account].&[3]+[Account].&[4] T
I want to know how to write the regular expression in javascript. Please help
How to write a regular expression in javascript that must follow the conditions All
I am try to write a regular expression in javascript that will not allow
I am having trouble trying to write a regular expression in JavaScript that can
Greetings JavaScript and regular expression gurus, I want to return all matches in an
I need to write a regular expression that finds javascript files that match <anypath><slash>js<slash><anything>.js
I would like to write a regular expression in javascript to match specific text,
Can anyone tell me how to write javascript code that removes everything after the

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.