Created
March 17, 2015 01:40
-
-
Save marcysutton/6c1ccf408eb1d0417fdc to your computer and use it in GitHub Desktop.
JavaScript string manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works only if there's 1 '?'. For example 1?1? should return ['1010', '1011', '1110', '1111'], instead returns ['1010', '1010', '1111', '1111']