Reputation: 771
I want to remove a string inside a comma delimited string and I want to do this via regex.
var strToReplace = 'bbb';
var str = 'values["aaa","bbb","ccc"]';
The problem I encountered was the commas. Replacing only the desired string leaves the commas behind so I added removing also the following comma if present.
var replaceRegexp = new RegExp('"'+ strToReplace +'",?');
str = str.replace(replaceRegexp,'');
However it leaves a trailing comma if it was the last item. Adding it to the front will have the same problem if removing the first item and adding to both sides will remove the commas when removing from the the middle items.
I just added another replace string to remove the trailing one after removing the item.
str = str.replace('",]','"]');
Can someone point me to a more elegant way to handle this in just one regExp. Thanks
EDIT:
var str = 'values["aaa","bbb","ccc"]';
var str = 'values["bbb"]';
var str = 'values["aaa","bbb","ccc","bbb"]';
Upvotes: 1
Views: 1679
Reputation: 766
This is the regex used in my javascript code and it works for me, given finding string is "findme":
/^findme,|,findme$|,findme(?=,)/
And code snippet like this:
var find = form1.find.value;
var source = form1.source.value;
var regex = new RegExp("^"+find+",|,"+find+"$|,"+find+"(?=,)");
var result = source.replace(regex, "");
Upvotes: 0
Reputation: 1086
This is optimimal in my opinion.
var strToReplace = 'bbb',
re = new RegExp('(,?)"' + strToReplace + '"(,?)', 'g'),
str = 'values["bbb","aaa","ccc","bbb","ccc","aaa","bbb"]';
str = str.replace(re, function(match, sub1, sub2) {
if (!sub1.length || !sub2.length) {
return '';
} else {
return ',';
}
});
document.getElementById('result').textContent = str;
console.log(str);
<div id="result"></div>
Upvotes: 1
Reputation: 174696
You need to use alternation operator |
.
String you want to replace is at the middle
> var strToReplace = 'bbb';
undefined
> var str = 'values["aaa","bbb","ccc"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'
String you want to replace is at the last
> var str = 'values["aaa","ccc","bbb"]';
undefined
> var strToReplace = 'bbb';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'
String you want to replace is at the first
> var strToReplace = 'bbb';
undefined
> var str = 'values["bbb","aaa","ccc"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'
|
called alternation or logical or operator. But the logical OR operator is a must needed one.
At first, the regex engine would try to match all the chars using the pattern before the |
symbol. After that, it tries to match the remaining characters (in this case) using the pattern which was next to the |
operator.
So the first part of the above regex "bbb",
matches all the bbb,
strings but it fails to match the last bbb
because it isn't have a following comma. Then the next pattern ",bbb"
comes into attack and matches the last ,bbb
string.
OR
YOu could use the below regex if you want to remove "bbb"
if the list has only "bbb"
> var str = 'values["bbb"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,?"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values[]'
Upvotes: 3
Reputation: 67968
"bbb",|,?"bbb"
You can try something like this which will fulfll all conditions.See demo.
https://regex101.com/r/vN3sH3/40
Upvotes: 2