Remove value from comma delimited string

This is a discussion on "Remove value from comma delimited string" within the JavaScript Forum section. This forum, and the thread "Remove value from comma delimited string are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 8th, 2008, 04:22
New Member
Join Date: Nov 2007
Location: USA
Age: 35
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Remove value from comma delimited string

I have a string: A-001, A-003, A-004, A-0016

This is what i'm doing to remove a value(strValue) from this string:

var strPatternComma = new RegExp(', ' + strValue, 'gi');
tempText = tempText.replace(strPatternComma , '');
var strTextPattern = new RegExp(strValue, 'gi');
tempText = tempText.replace(strTextPattern , '');

I have a small problem with it. Let's say i need to remove value "A-001" from it, it does remove it but it also removes a "A-0016" part of the "A-0016".

Please help, thanks
Reply With Quote

  #2 (permalink)  
Old Feb 8th, 2008, 04:41
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Remove value from comma delimited string

If you construct your patter and don't remove the commas, you can just search for the commas on either side. If you need the commas to remain, use backreferences in your replace.

Code: Select all
var strPattern = new RegExp('(,[\s]{1})(' + strValue + ',)', 'gi');

tempText = tempText.replace(strPattern, '$1');
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Feb 8th, 2008 at 05:39.
Reply With Quote
  #3 (permalink)  
Old Feb 8th, 2008, 05:28
New Member
Join Date: Nov 2007
Location: USA
Age: 35
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Remove value from comma delimited string

Quote:
Originally Posted by Rakuli View Post
If you construct your patter and don't remove the commas, you can just search for the commas on either side. If you need the commas to remain, use backreferences in your replace.

Code: Select all
 
var strPattern = new RegExp('(, )(' + strValue + ',)', 'gi');
 
tempText = tempText.replace(strPattern, '$1');
Sorry not clear for me, can you elaborate it a little. Also there is a possibility that one of the values may have a comma inside of it as well., like this(in red): A-001, B-CD, A,BC, A-003
Reply With Quote
  #4 (permalink)  
Old Feb 8th, 2008, 05:39
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Remove value from comma delimited string

Well I assume that strValue contains the value that you are looking for so all you need to do is construct the pattern so that it matches the commas on either side. Then when you replace, you replace the pattern.

Let's say strValue contains A,BC

The patter looks like this

(,[\s]{1})(A,BC,)

The first set of parenthesis is looking for a comma followed by one space. The next lot searches for the strValue (A,BC) followed immediately by a comma.

When you are using the replace function you can use backreferences in your replace string. $1 will represent the first set of parenthesis $2 the second etc.

So you replace string looks like this '$1' which basically means replace the whole thing with what was matched in the first set of brackets ', '.. This will mean the value will be taken out literally and not part of another string because it is matching against the comma that comes after it.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Feb 13th, 2008, 21:06
New Member
Join Date: Nov 2007
Location: USA
Age: 35
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Remove value from comma delimited string

Quote:
Originally Posted by Rakuli View Post
Well I assume that strValue contains the value that you are looking for so all you need to do is construct the pattern so that it matches the commas on either side. Then when you replace, you replace the pattern.

Let's say strValue contains A,BC

The patter looks like this

(,[\s]{1})(A,BC,)

The first set of parenthesis is looking for a comma followed by one space. The next lot searches for the strValue (A,BC) followed immediately by a comma.

When you are using the replace function you can use backreferences in your replace string. $1 will represent the first set of parenthesis $2 the second etc.

So you replace string looks like this '$1' which basically means replace the whole thing with what was matched in the first set of brackets ', '.. This will mean the value will be taken out literally and not part of another string because it is matching against the comma that comes after it.
Thank you very much for explaining it. I still can not get it right, it looks i'm doing something wrong.

To simplify it, i delimit string with semi-column(; ) instead of comma(,) .
This is what i have right now:
var strPattern = new RegExp('(;[\s]{1})(' + strValueName + ')');
tempText = tempText.replace(strPattern, '$2');

and these are parameters:
strValueName = A-002
tempText = A(1-7); A-00000762; A-0001; A-001; A-0026; A-002; A-003; A-007; A-007, Tigris; A-02056; A-1 polyphenols, PhytoMedical

Is there something wrong with my RegEx?
Reply With Quote
  #6 (permalink)  
Old Feb 14th, 2008, 16:12
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Remove value from comma delimited string

Try escaping the semi colon with a backslash.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove frames camarun20 Web Page Design 6 Mar 26th, 2008 14:11
Space on right of site, cant remove it weasel Web Page Design 16 Jan 12th, 2008 14:26
Result = 100000. How to add comma so reads: 100,000 Andy K Flash & Multimedia Forum 0 Jan 16th, 2006 13:58
Remove UL spacing problem relph2 Web Page Design 3 Jan 3rd, 2006 17:22
remove redirection expressweb Implemented Suggestions 6 Jul 21st, 2005 22:24


All times are GMT. The time now is 22:50.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43