Skip to content

Instantly share code, notes, and snippets.

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 mreis1/6ee99d01ecc74585c1d51d0fb20cb465 to your computer and use it in GitHub Desktop.
Save mreis1/6ee99d01ecc74585c1d51d0fb20cb465 to your computer and use it in GitHub Desktop.
JS RegExp Examples
var str = 'FFA0000000'; // Our starting string where we want to remove all '00'
var reg = /[0]{2,2}$/;
var match = reg.exec(str);
while(match = reg.exec(str)){
var x = str.split('');
x.splice(match.index,2);
str = x.join('');
console.log(str, match)
}
// Loop 1: FFA00000 ["00", index: 8, input: "FFA0000000", groups: undefined]
// Loop 2: FFA000 ["00", index: 6, input: "FFA00000", groups: undefined]
// Loop 3: FFA0 ["00", index: 4, input: "FFA000", groups: undefined]
Result: FFA0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment