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.
|
|
|
|
|
![]() |
||
Remove value from comma delimited string
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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 |
|
|
|
||||
|
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.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Last edited by Rakuli; Feb 8th, 2008 at 05:39. |
|
|||
|
Re: Remove value from comma delimited string
Quote:
|
|
||||
|
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)
|
|
|||
|
Re: Remove value from comma delimited string
Quote:
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? |
|
||||
|
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)
|
![]() |
| Thread Tools | |
|
|
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 |