按后缀删除单词

Remove Words By Suffix

本文关键字:单词 删除 后缀      更新时间:2023-09-26

我正在尝试编写一个简单的JavaScript函数来删除字符串中带有特定后缀的单词的所有出现。

function removeClassBySuffix(s, suffix) {
    var regx = new RegExp(what-regex-to-put-here, 'g');
    s = s.replace(regx, '');
    return s;
}
/* new RegExp('''b.+?' + suffix + '''b', 'g') -- doesn't work */

所以

removeClassBySuffix('hello title-edit-panel deal-edit-panel there', '-edit-panel');
// Should return 'hello   there'.

请帮忙?

我没有尝试过,但我认为以下内容应该有效:

new RegExp('''b''S+?' + suffix + '''b', 'g')

像这样的事情怎么样:

function removeClassBySuffix(s, suffix) {
    var a = s.split( ' ' ),
    result = [];
    for (i in a)
        if ( a[i].indexOf( suffix ) != ( a[i].length - suffix.length ) )
            result.push( a[i] );
    return result.join(' ');
}