let str = ` * hello* * world* * 678* * {}:"* ` let r = str.replace(/(\*\s+[^*]+\*)/g, function(a) { return /^\w/.test(a) ? a.replace(/\s/g, '') : a }) console.log(r)