It is actually quite simple.
Assuming str is the line you clear:
str = str.replace(/[^a-z0-9+]+/gi, '+');
^ means "nothing in this list of characters." + after the group [...] means "one or more". /gi means "replace whatever you find, regardless of case."
Thus, any number of characters that are not letters, numbers or "+" is converted to one "+".
To remove the substrings in brackets (as indicated in the comments), do this replacement first:
str = str.replace(/\(.+?\)/g, '');
function replacer() { var str = document.getElementById('before').value. replace(/\(.+?\)/g, ''). replace(/[^a-z0-9+]+/gi, '+'); document.getElementById('after').value = str; } document.getElementById('replacem').onclick = replacer;
<p>Before: <input id="before" value="Durarara!!x2 Ten" /> </p> <p> <input type="button" value="replace" id="replacem" /> </p> <p>After: <input id="after" value="" readonly /> </p>
source share