Javascript 如何从字符串中删除列表短语?
我想检查字符串中的短语列表。如果这些短语存在,我想删除它们并返回一个新字符串Javascript 如何从字符串中删除列表短语?,javascript,arrays,string,underscore.js,Javascript,Arrays,String,Underscore.js,我想检查字符串中的短语列表。如果这些短语存在,我想删除它们并返回一个新字符串 For Example: String: Lower Dens - Propagation (Official Music Video) Result: Lower Dens - Propagation 这是我到目前为止所做的,但它不起作用。这适用于单个单词,但不适用于短语。我对每个循环使用下划线,但我对任何解决方案都持开放态度 formatVideoTitle = function(videoTitle){
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;
};
编辑:更改为
newVideoTitle = newVideoTitle.replace(phrase, '');
“replace”返回操作的结果,您需要将其传回。还要注意,这将只匹配该字符串的第一次出现,如果该短语出现多次,则需要使用正则表达式替换所有实例
编辑2:使用正则表达式替换所有实例的示例:
newVideoTitle = newVideoTitle.replace(new RegExp(phrase, 'g'), '');
将newVideoTitle设置为其工作的替换操作的结果。我想做一个JSFIDLE,但我太懒了,不想包含下划线
replace
返回一个值,因此您需要将被替换的变量分配给一个新变量(或相同的变量):
我认为问题在于replace语句。如果将结果分配给newVideoTitle,则它将起作用
newVideoTitle = newVideoTitle.replace(phrase, '');
由于JavaScript中缺少“replaceAll”,我喜欢使用“split/join”
如果您知道短语始终位于字符串的后面,还可以使用:
formatVideoTitle = function(videoTitle){
...
_.each(phrases, function(phrase){
newVideoTitle = newVideoTitle.slice(0, newVideoTitle.indexOf(phrase));
});
return newVideoTitle;
};
如果您不介意不使用下划线,JavaScript有一种相对简单的方法来实现正则表达式的相同功能(尽管创建正则表达式有点复杂): 参考资料:
下划线
。您在javascript控制台中看到错误了吗?您是否有任何示例字符串向我们展示(原始和修订的('before'/'after')版本)?从外观上看,你想从所有东西中删除(视频)
?我不明白你想做什么。你能发布更多关于所需输入/输出的信息吗?用答案回答问题,而不是另一个问题。我建议解释一下你的版本和他的版本之间的差异,解释一下为什么他的版本是错的,而你的版本是对的。另外,我的方法与你的方法不同,因此你必须调整它。很抱歉,为了获得我的第一个分数,我还需要比其他方法更快地响应。不管怎样,你现在至少从三个独立的来源得到了正确的答案:-)“replaceAll”问题的糟糕解决方案。使用正则表达式解决方案。。。newVideoTitle=newVideoTitle.replace(新的RegExp(短语“g”),“”);
_.each(phrases, function(phrase) {
newVideoTitle = newVideoTitle.replace(phrase, '');
});
newVideoTitle = newVideoTitle.replace(phrase, '');
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;
};
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)"));