有没有办法压缩以下正则表达式替换? (javascript)

Is there a way to condense the following regex replace? (javascript)

我正在尝试从输入字符串的开头和结尾删除白色space,将超过 1 个 space 替换为一个 space and 删除所有特殊字符。该片段(随附)有效,但我只是想问一下是否有办法让这个看起来稍微不那么呃......丑陋?一定有更好的写法,对吧?

const filterInput = (vals) => {
  console.log(vals.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ').replace(/[^\w ]/g, ""));
};

filterInput(" squid*^%ward     sponge((bob        ")

trim() 字符串第一个,而不是使用第一个 .replace:

const filterInput = (vals) => {
  const result = vals
    .trim()
    .replace(/\s+/g, ' ')
    .replace(/[^\w ]/g, '');
  console.log(result);
};

filterInput(" squid*^%ward     sponge((bob        ")

您可以将其缩减为单个正则表达式,但 IMO 的可读性不佳:

const filterInput = (vals) => {
  const result = vals
    .trim()
    .replace(/\s+|\W/g, s => s[0] === ' ' ? ' ' : '');
  console.log(result);
};

filterInput(" squid*^%ward     sponge((bob        ")