如何替换附加到另一个字符串的字符串?

Sol*_*444 0 javascript string node.js

我有一个来自数据源的长描述.数据可以是5,000个字符加.我们有一个不需要的简短的一行描述字段.当它没有填写时,我们使用描述中的前128个字符并将"..."附加到最后三个字符.所以125是描述,最后三个是"......".

我们遇到了文本到语音的问题,其中附带作品的"......"出错了.例如,短语可以看起来像"美丽的家庭本地......".

我想找到"...",然后找到"附加"它的单词(通过触摸单词为无空格)并用"截断"或"查看完整描述"的行替换它.

我知道替换,但只需要一个硬字符串,所以我只是替换"..."而不是它和它附加的单词.

预期结果的一些例子:

welcome to this beautiful home -> welcome to this beautiful home
welcome to this beautiful h... -> welcome to this beautiful truncated
welcome to this beautiful ... -> welcome to this beautiful truncated
Run Code Online (Sandbox Code Playgroud)

我怎样才能在JavaScript中实现这一目标?

sli*_*der 5

String replace确实考虑正则表达式.所以你可以这样做:

let strs = [
  'welcome to this beautiful home',
  'welcome to this beautiful h...',
  'welcome to this beautiful ...'
];

strs.forEach(s => console.log(s.replace(/\w*\.{3}/g, 'truncated')));
Run Code Online (Sandbox Code Playgroud)