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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:36+00:00 2026-05-27T15:01:36+00:00

I have tried to delete an item from a string divided with commas: var

  • 0

I have tried to delete an item from a string divided with commas:

var str="this,is,unwanted,a,test";

  • if I do a simple str.replace('unwanted',''); I end up with 2 commas
  • if I do a more complex str.replace('unwanted','').replace(',,','');
    It might work

But the problem comes when the str is like this:

var str="unwanted,this,is,a,test"; // or "...,unwanted"

However, I could do a ‘if char at [0 or str.length] == comma’, then remove it

But I really think this is not the way to go, it is absurd I need to do 2 replaces and 2 ifs to achieve what I want

I have heard that regex can do powerful stuff, but I simply can’t understand it no matter how hard I try

Important Notes:

  • It should match after OR before (not both), or we will end with
    “this,is,,a,test”
  • There are no spaces between commas
  • 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-27T15:01:37+00:00Added an answer on May 27, 2026 at 3:01 pm

    How about something less flaky than a regex for this sort of replacement?

    str = str
         .split(',')
         .filter(function(token) { return token !== 'unwanted' })
         .join(',');
    

    jsFiddle.

    However if you are convinced a regex is the best way…

    str = str.replace(/(^|,)?unwanted(,|$)?/g, function(all, leading, trailing) {
        return leading && trailing ? ',' : '';
    });
    

    (thanks Logan F. Smyth.)

    jsFiddle.

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

Sidebar

Related Questions

I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried this: foreach (ListViewItem item in lvPC.CheckedItems) { selectedTag = item.Tag.ToString(); DialogResult
I am trying to figure out how to delete one item from this NSMutableArray
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
Hi a am struggling with this part. I want to simply delete an item
I have a python function defined as follows which i use to delete from
I have to delete child item in my expandablelistview. My attempt was: 1. Data
I keep getting this error when trying to delete a record from my table.
I have tried to document this as well as I can in the code.
I have an item template that I did wrong and want to delete. I

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.