如何从字符串中删除列表短语?

时间:2014-08-26 15:57:26

标签: javascript arrays string underscore.js

我想检查字符串中的短语列表。如果存在这些短语,我想删除它们并返回一个新字符串。

For Example:
String: Lower Dens - Propagation (Official Music Video)
Result: Lower Dens - Propagation

这是我到目前为止所做的,但它不起作用。这适用于单个单词,但不适用于短语。我在每个循环中使用下划线,但我对任何解决方案持开放态度。

formatVideoTitle = function(videoTitle){
    var phrases = [
        '(Official Music Video)',
        '(Official Video)',
        '(Music Video)'
    ],
    newVideoTitle = videoTitle;

    _.each(phrases, function(phrase){
        newVideoTitle.replace(phrase, '');
    });

    return newVideoTitle;
};

6 个答案:

答案 0 :(得分:1)

将newVideoTitle设置为其工作的替换操作的结果。我会做一个jsfiddle,但是我懒得包含下划线。

heres a fiddle

formatVideoTitle = function(videoTitle){
    var phrases = [
        '(Official Music Video)',
        '(Official Video)',
        '(Music Video)'
    ],
    newVideoTitle = videoTitle;

    _.each(phrases, function(phrase){
        newVideoTitle = newVideoTitle.replace(phrase, '');
    });

    return newVideoTitle;
};

答案 1 :(得分:0)

formatVideoTitle = function(videoTitle){
    var phrases = [
        '(Official Music Video)',
        '(Official Video)',
        '(Music Video)'
    ],
    newVideoTitle = videoTitle;

    _.each(phrases, function(phrase){
        newVideoTitle = newVideoTitle.replace(phrase, '');
    });

    return newVideoTitle;
};

编辑:更改

newVideoTitle = newVideoTitle.replace(phrase, '');

'replace'返回操作的结果,您需要将其传回。还要注意,这只会匹配该字符串的第一次出现,如果该短语出现多次,那么您将需要使用正则表达式来替换所有实例。

编辑2:使用正则表达式替换所有实例的示例:

newVideoTitle = newVideoTitle.replace(new RegExp(phrase, 'g'), '');

答案 2 :(得分:0)

replace返回一个值,因此您需要将替换的变量分配给一个新变量(或同一个变量):

_.each(phrases, function(phrase) {
  newVideoTitle = newVideoTitle.replace(phrase, '');
});

DEMO

答案 3 :(得分:0)

我认为问题在于替换语句。如果将结果分配给newVideoTitle,那么它将起作用。

newVideoTitle = newVideoTitle.replace(phrase, '');

答案 4 :(得分:0)

由于缺少" replaceAll"在JavaScript中,我喜欢使用" split / join"。

formatVideoTitle = function(videoTitle){
    ...
    _.each(phrases, function(phrase){
        newVideoTitle.split(phrase).join('');
    });

    return newVideoTitle;
};

如果你知道短语总是在字符串的后面,你也可以使用:

formatVideoTitle = function(videoTitle){
    ...
    _.each(phrases, function(phrase){
        newVideoTitle = newVideoTitle.slice(0, newVideoTitle.indexOf(phrase));
    });

    return newVideoTitle;
};

答案 5 :(得分:0)

如果你不介意不使用下划线,JavaScript有一个相对简单的方法来实现与正则表达式相同(虽然在创建该正则表达式时有点复杂):

formatVideoTitle = function (title) {
    var phrases = [
        '(Official Music Video)',
        '(Official Video)',
        '(Music Video)'],
    // creating Regular Expression object, by joining the phrases together with '|'
    // characters (an 'OR' relationship) to create a string, prefacing the '(' and ')'
    // characters with a double escape (to escape them in the created RegExp), and
    // wrapping that created string in '(' and ')' to create a matching group:
        reg = new RegExp('(' + phrases.join('|').replace(/(\(|\))/g, function(a){
            return '\\' + a;
        }) + ')', 'gi');
    // returning the amended title:
    return title.replace(reg, '');
}

console.log(formatVideoTitle("Lower Dens - Propagation (Official Music Video)"));

JS Fiddle demo

参考文献: