2

I have a few signal flags in a serialized string and I need to remove the flag from the string when the user requests a certain operation. I was unable to find a regex that works in all cases for the below string.

var flags = "out:ab,bc,bcc,cd";

This is the closest I got (e.g. removing bc flag)

flags.replace(/[:,]bc\b/, "");

Result is out:ab,bcc,cd which is cool, but when removing ab I will get outbc,bcc,cd which is wrong. The result must always keep the string serialized, e.g. out:flag1,flag2 I tried capturing subpatterns but could not find an all-round working combo.

-- more info:

Flags are 0/1 signals, if a flag is present I must remove it.

Out: is the operation mode and it's the opposite of In: (this is a second group of flags). To put it simple, Out: is a "allow all, but blacklist these", where In: is "deny all, but whitelist these". The app can run in either mode. ab, bc, cd ... are sample alphabetical keys.

3
  • So what are the "flags"? What's out? What's the expected output? That would clear things a bit. Commented Jun 11, 2013 at 21:57
  • If you want to match something but not replace it, use lookaround Commented Jun 11, 2013 at 21:59
  • @elclanrs I added more info hope it's clearer now. Barmar I need to remove the flag, not just match :) Commented Jun 11, 2013 at 22:19

1 Answer 1

2

Use non-capturing parentheses:

var q = ['cd', 'bc'].join('|'),
    re = new RegExp('(?:[:]('+q+'))|(,('+q+')(?=,))|(,('+q+')$)', 'g'),
    flags = "out:ab,bc,bcc,cd";
flags.replace(re, '');
=> "out:ab,bcc"

From MDN's Regular Expressions:

(?:x) Matches 'x' but does not remember the match. The parentheses are called non-capturing parentheses, and let you define subexpressions for regular expression operators to work with. Consider the sample expression /(?:foo){1,2}/. Without the non-capturing parentheses, the {1,2} characters would apply only to the last 'o' in 'foo'. With the capturing parentheses, the {1,2} applies to the entire word 'foo'.


Edit: An explanation of the RegExp

/(?:[:](cd|bc))|(,(cd|bc)(?=,))|(,(cd|bc)$)/g

There are three sections, separated by pipes. ?:[:](cd|bc) covers flags right after the semicolon; ,(cd|bc)(?=,) covers flags between commas, including the preceding comma in the matched expression; ,(cd|bc)$ covers flags at the end of the string, also including the preceding comma.


Edit 2:

Fixed RegExp, replace q with the flags you want to match.

Sign up to request clarification or add additional context in comments.

4 Comments

Hm removing ab still removes :
I think I got it: /(?:\bab\b(?:,)?)|(,ab(?=,))|(,ab$)/g I changed the first section and removed the ORs since there's no use case that would need removing two or more at a time.
Also, /((?:ab|bcc|cd),)|(,(?:ab|bcc|cd)(?=,))|(,?(?:ab|bcc|cd)$)/g --> jsfiddle.net/dXtDu
Cool, I added some additional groups and they all seem to be processed correctly. jsfiddle.net/dXtDu/1 Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.