-1

I have a string with potentially random characters in it. I wanting to replace all instances of a section with wildcard support. Example:

var input = 'abcdef acbdef acdbef';

input = coolFunction(input, 'a*b', '_'); 
// I want to replace every charachter between an a and the next closest b with _'s
//Output should be '__cdef ___def ____ef'

Can someone tell me how I can do this?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

0

Try

let input = 'abcdef acbdef acdbef';
let wild = 'a*b';

let re = new RegExp(wild.replace(/\?/,'.').replace(/\*/,'.*?'),'g');
let output = input.replace(re, c => '_'.repeat(c.length)); 

console.log(output);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • Check your output and then check the requested output. They want an underscore for each letter they substitute. So three underscores in the second example and four in the third. – Federico klez Culloca Aug 21 '19 at 16:38