Skip to content

Instantly share code, notes, and snippets.

@marcysutton
Created March 17, 2015 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcysutton/6c1ccf408eb1d0417fdc to your computer and use it in GitHub Desktop.
Save marcysutton/6c1ccf408eb1d0417fdc to your computer and use it in GitHub Desktop.
JavaScript string manipulation
var str = '1?101?011';
var repl = ['0', '1'];
var pattern = /\?/g;
var numMatches = str.match(pattern).length;
for(var j=0; j<numMatches; j++){
for(var i=0; i<repl.length; i++){
console.log(str.replace(pattern, repl[i]));
}
}
@skarakash
Copy link

Works only if there's 1 '?'. For example 1?1? should return ['1010', '1011', '1110', '1111'], instead returns ['1010', '1010', '1111', '1111']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment