让我们坐下来,我有句子“我喜欢饼干”和句子“I_like_chocolate_cookies”。如何拆分字符串“我喜欢饼干”并检查第二句中是否存在单词?
问问题
112 次
4 回答
1
像这样
var words = "I like cookies".replace(/\W/g, '|');
var sentence = "I_like_chocolate_cookies";
console.log(new RegExp(words).test(sentence));
于 2012-08-15T12:11:27.840 回答
0
这是一些示例代码:
str1 = 'I like cookies'
str2 = 'I_like_chocolate_cookies'
// We convert the strings to lowercase to do a case-insensitive check. If we
// should be case sensitive, remove the toLowerCase().
str1Split = str1.toLowerCase().split(' ')
str2Lower = str2.toLowerCase()
for (var i = 0; i < str1Split.length; i++) {
if (str2Lower.indexOf(str1Split[i]) > -1) {
// word exists in second sentence
}
else {
// word doesn't exist
}
}
希望这可以帮助!
于 2012-08-15T12:11:19.293 回答
0
像这样?
var x ="i like grape";
var c ="i_don't_like";
var xaar = x.split(' ');
for(i=0;i<xaar.length;i++){
if(c.indexOf(xaar[i])>-1) console.log(xaar[i]);
}
于 2012-08-15T12:11:19.363 回答
0
var foo = "I_like_chocolate_cookies";
var bar = "I like chocolate cookies";
foo.split('_').filter(function(elements) {
var duplicates = []
if(bar.split().indexOf(element) != -1) {
return true;
}
});
于 2012-08-15T12:32:48.877 回答