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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:17:37+00:00 2026-06-10T13:17:37+00:00

Suppose I have string variable: var animals = catdog caT dog cat; I don’t

  • 0

Suppose I have string variable:

var animals = "catdog caT dog cat";

I don’t want cats, big caTs, and messy whitespaces. I’ve tried to use:

var rep = "cat"
var nocats = animals.replace(new RegExp(rep, 'g'), '');

and nocats is now “dog caT dog “, while I need “catdog dog”

What regexp should I use ?

  • 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-10T13:17:39+00:00Added an answer on June 10, 2026 at 1:17 pm

    I note that your question is not entirely clear on how exactly you would like whitespaces in the string to be treated. Assuming you want to collapse the whitespace in the source string, this should work:

    // " catdog      caT  dog    cat" --> "catdog dog" 
    animals.replace(new RegExp('\\s*\\b' + animal + '\\b\\s*|\\s+', 'gi'), ' ').trim();
    

    Also make sure you are aware of the semantics of the word boundary assertion – “cat-dog” will be replaced to “-dog” in this case. An alternative will be

    // " cat_dog cat-dog cat dog catdog catcatdog " --> "cat_dog cat-dog dog catcatdog" 
    animals.replace(new RegExp('(^|\\s+)' + animal + '(\\s+|$)|\\s+', 'gi'), ' ').trim();
    

    For reference: What is a word boundary in regexes?

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

Sidebar

Related Questions

In Java, suppose I have a String variable S, and I want to search
I have a Big html in String variable and I want to get contents
Suppose I have a string 'johndoe@hotmail.com' . I want to store the string before
Suppose I have a string 04032010. I want it to be 04/03/2010. How would
suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
suppose i have xml data stored in string variable. so when i am trying
I have a Date variable startdt and a String variable hdnsdate . Suppose if
Suppose I have a string of integers separated by commas of variable length. What
I am confused between NSString and NSMutable string usage. Suppose I have instance variables
Suppose I have string Name and Image Photo as properties of a class in

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.