简体   繁体   English

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

[英]How do I remove a list phrases from a string?

I want to check a string for a list of phrases. 我想检查一个字符串以获取短语列表。 If those phrases exist I want to remove them and return a new string. 如果这些短语存在,我想删除它们并返回一个新字符串。

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

This is what I have so far, but it does not work. 到目前为止,这是我所拥有的,但是不起作用。 This would work for single words, but not phrases. 这将适用于单个单词,但不适用于短语。 I am using underscore for the each loop, but I am open to any solution. 我在每个循环中都使用下划线,但是我愿意接受任何解决方案。

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

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

    return newVideoTitle;
};

set newVideoTitle to the result of the replace operation it works. 将newVideoTitle设置为它起作用的替换操作的结果。 I would make a jsfiddle but am too lazy to include underscore. 我会做一个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;
};
formatVideoTitle = function(videoTitle){
    var phrases = [
        '(Official Music Video)',
        '(Official Video)',
        '(Music Video)'
    ],
    newVideoTitle = videoTitle;

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

    return newVideoTitle;
};

Edit: The change is 编辑:更改是

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

'replace' returns the result of the operation, you you'll need to pass it back. “替换”返回操作结果,您需要将其传递回去。 Also beware that this will only match the first occurance of that string, if the phrase appears multiple times then you'll need to use regex to replace all instances. 还要注意,这只会匹配该字符串的第一次出现,如果该短语出现多次,则需要使用正则表达式替换所有实例。

Edit 2: Example of use of regex to replace all instances: 编辑2:使用正则表达式替换所有实例的示例:

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

replace returns a value, so you need to assign the replaced variable to a new variable (or the same one): replace返回一个值,因此您需要将替换后的变量分配给新变量(或相同的变量):

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

DEMO DEMO

I think the problem is with the replace statement. 我认为问题在于替换语句。 If you assign the result to newVideoTitle than it will work. 如果将结果分配给newVideoTitle,它将起作用。

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

Due to the lack of "replaceAll" in JavaScript, I like to use "split/join". 由于JavaScript中缺少“ replaceAll”,因此我喜欢使用“ split / join”。

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

    return newVideoTitle;
};

If you know that the phrase is always at the back of the string, you could also use: 如果您知道该短语始终位于字符串的末尾,则还可以使用:

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

    return newVideoTitle;
};

If you don't mind not using underscore for this, JavaScript has a relatively easy means of achieving the same with regular expressions (though there's a little complexity in the creation of that regular expression): 如果您不介意不使用下划线,则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 . JS小提琴演示

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM