I am given two variables, a string [var str] and an array of words [var exceptions]. I am replacing every middle character of every word longer than 3 letters with an asterisk using the following regular expression:
var edited = str.replace(/\B\w\B/g, '*');
var exceptions = ["example","doing"]
I'd probably turn the array of excludes into a map so that I benefit from faster checking if a word is in the array. Then I'd use the fact that the replace
function accepts a function for the replacement, and make the decision in there:
var exclude = ["example", "what"];
var str = "This is an example of what I am doing";
var map = Object.create(null);
exclude.forEach(function(entry) {
map[entry] = true;
});
var edited = str.replace(/\b(\w)(\w+)(\w)\b/g, function(m, c0, c1, c2) {
return map[m] ? m : c0 + "*".repeat(c1.length) + c2;
});
console.log(edited);
I've used String#repeat
in the above, which is from ES2015, but can be easily shimmed for older browsers. Or use c1.replace(/./g, "*")
instead.
Here's an ES2015+ version, using Set
rather than an object map:
let exclude = ["example", "what"];
let str = "This is an example of what I am doing";
let set = new Set();
exclude.forEach(entry => {
set.add(entry);
});
let edited = str.replace(/\b(\w)(\w+)(\w)\b/g, (m, c0, c1, c2) =>
set.has(m) ? m : c0 + "*".repeat(c1.length) + c2
);
console.log(edited);