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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:27:32+00:00 2026-05-28T04:27:32+00:00

Ok, I’m feeling pretty thick right now. Basically, I want to define a JavaScript

  • 0

Ok, I’m feeling pretty thick right now. Basically, I want to define a JavaScript regular expression that will match anything except precisely a particular string. So say I have the string

"dog"

or

"cat"

I want a single standalone regular expression such that it will match the string

"dogsled"

or the string

"cattle"

Just not “dog” or “cat” on its own. I’ve tried this, which basically says, ignore anything beginning with “cat” or “dog”, which is not exactly what I need…

var pattern= /^(?!dog|cat).+/

pattern.test("cat") // false, as expected
pattern.test("dog") // false, as expected
pattern.test("bananananana") // true
pattern.test("dogsled") // false, but the regexp I want would return true

This has to be simple…. thanks!

Edit
Just to clarify, I don’t want to do any negation of return values to get the result I want- the regex should return false for “dog” and true for “dogsled” or false for “cat” and true for “cattle”

  • 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-28T04:27:33+00:00Added an answer on May 28, 2026 at 4:27 am

    I would opt for simply negating a match as noted in Linus Kleen’s answer. However, if you absolutely must do it all in regex (or for the learning experience or something) then I think the following should work:

    ^((dog|cat(?!$)).+|(?!dog|cat).+)$
    

    This uses two negative lookaheads in an alternation. The first says “Match cat or dog if it is not followed by the end of string character ($), followed by anything else”. This matches dogsled and cats and things of that nature.

    The second half of the alternation says “Make sure the beginning of the string (^) is not followed by dog or cat (so it doesn’t start with either of those), then match anything”. This gets you any word that doesn’t begin with cat or dog (like banana).

    Here’s an example on regexpal.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.