2013-08-26 73 views
2

是否有内置的JavaScript字符串方法,可以帮助我微调此代码以确保它只能找到与名称完全匹配的内容?Javascript精确搜索+找到名称的完全匹配

这是我的代码。

/*jshint multistr:true */ 

var text = "Sid quick brown fox jumps over the lazy dog "; 
var myName = "Sid"; 
var hits = []; 

for (var i=0; i< text.length; i++){ 
    if(text[i] === 'S'){ 
     for(var j=i; j < i + myName.length; j++){ 
      hits.push(text[j]); 
     } 
    }else{ 
     console.log("Your name wasn't found!") 
    } 

} 
console.log(hits); 
+1

为什么不能像@BjörnRoberg建议的那样使用正则表达式?这只是为了... – philipp

回答

0

反正继承人一些更好的代码

check(0); 

function check(start){ 
if (text.subStr(start).indexOf(myName) >= 0){ 
hits++; 
    check(text.subStr(start).indexOf(myName)); 
} 
} 
if (hits < 1){ 
    console.log("Your name wasn't found!") 
} 
console.log(hits); 

仍然还没有,如果它的工作原理,但其总体思路

所以答案是“有一个内置的JavaScript字符串的方法,可以帮助我确认(你)微调这个代码,以确保它只能找到完全匹配的名称。“

0

你可以尝试使用正则表达式:

var text = "Sid quick brown fox jumps over the lazy dog "; 
var myName = "Sid"; 

var re = new RegExp(myName); 

re.exec(text); 
// => ["Sid"] 

,或者如果你一定想有一个字符串的方法:

myName.match(re); 
0

我不知道这是不是比一些其他的答案都不同,但这是我如何做到这一点,而不使用RegEx,并仍然填充所有出现的字符串数组变量。同样,我只使用substring()的String方法来完成它,这使得这是另一个可能的答案。希望这有助于:

var text = "blah bhlekm kdnclwi Christian blah blah, lots of blah in this text, blahChristian got one more Christian, and that's it."; 
var myName="Christian"; 
var hits = []; 
for (i=0; i<text.length; i++){ 
    if (text.substring(i, i+ myName.length) === myName){ 
      hits.push(text.substring(i, i+myName.length)); 
     } 
    } 

    if (hits.length == 0){ 
     console.log("Your name wasn't found!"); 
    }else{ 
     console.log("your name showed up " + hits.length + " times."); 
    } 
4

所以这里的另一个解决方案,使用功能match(),但更简单:

/*jshint multistr:true */ 
var text = "Hey, how are you doing Sanda? My name is Emily.\ 
Nice to meet you Sada. Where does your name come from, Sana?\ 
is Sanda a slavic name?"; 
/*var myName = "Sanda"; 
hits = []; 

for(var i=0; i < text.length; i++){ 
    if (text[i] === myName[0]) 
     for (var j=i; j< i + myName.length && j < text.length; j++) 
      hits.push(text[j]); 
} 

if (hits.length === 0) "Your name wasn't found!"; 
else console.log(hits);*/ 

var hits = text.match(/Sanda/g); 

if (hits.length === 0) "Your name wasn't found!"; 
else console.log(hits); 

语法是var <someVar> = <textToBeSearched>.match(/pattern/modifiers)。我的修饰符g适用于全局(搜索整个模式)。

此处了解详情: http://www.w3schools.com/jsref/jsref_match.asp http://www.w3schools.com/js/js_regexp.asp

干杯!

+0

我见过的最简化的版本,谢谢:) – phoenixlaef

1
var text = "Blah blah blah blah blah blah Hafeez \ 
blah blah blah Hafeez blah blah Hafeez blah blah \ 
blah blah blah blah blah Huzaif, Hazere"; 

var myName = "Hafeez"; 
var hits = []; 

for (var i =0; i < text.length ; i++) 
{ 
    if(text[i] === 'H') 
    { 
     for(var j = i; j < (myName.length + i); j++) 
     { 
      if (text.substring(i, (myName.length + i)) === myName) 
      { 
       hits.push(text[j]) 
      } 
     } 

    } 
} 

if(hits === 0) 
{ 
    console.log("Your name was'nt found"); 

} 
else 
{ 
    console.log(hits); 
    console.log(hits.length); 
} 
0

这是我自己的解决方案,采用串()函数和搜索()函数,这是检查在文中找到你要找的字一个内置的JavaScript字符串的方法。一旦成功,它将返回单词所在文本的位置,如果失败,则返回-1值。这只是另一种方式。

/*jshint multistr:true */ 
var text = "hey wassup mike? \ 
wanna play football with mike, maria and antonis?!??!??!?!"; 
var myName = "mike"; 
var hits = []; 
var pos = text.search(myName); 

while(pos !== -1) { 
    for (var j = 0; j < myName.length; j++) { 
      hits.push(myName[j]); 
     } 
    text = text.substring(pos+myName.length,text.length); 
    pos = text.search(myName); 
} 

if(hits === "") 
{ 
    console.log("Your name wasn't found!"); 
} 
else 
{ 
    for (var h = 0; h < hits.length; h++) { 
     console.log(hits[h]); 
    } 
} 
1

使用此参数只能匹配确切的名称,而不能使用部分名称。

var text = "Hello my name is Johny, are you Sandra, Sandra?"; 
 
var names = ["Sandra", "John"]; 
 

 
for (var i = 0; i < names.length; i++) { 
 
    var re = new RegExp("\\b" + names[i] + "\\b", "g"); 
 
    document.write("Matches for " + names[i] + ": " + (text.match(re) ? text.match(re).length : 0) + "<br>"); 
 
}

\b意味着正则表达式

1

的OP的例子是Codecademy网站,所以如果有人正在寻找相关的专门的Javascript答案单词边界:6/7课程这是我想出了什么:

/*jshint multistr:true */ 
var text = "How are you \ 
doing Sabe? What are you \ 
up to Sabe? Can I watch \ 
Sabe?"; 

var myName = "Sabe"; 

var hits = []; 

for (var i = 0; i < text.length; i++) { 
    if (text[i] === 'D') { 
     for (var j = i; j < (i + myName.length); j++) { 
      hits.push(text[j]); 
     } 
    } 
} 

if (hits.length === 0) { 
    console.log("Your name wasn't found!"); 
} else { 
    console.log(hits); 
} 

 

然后我决定像OP一样去做额外的挑战,那就是创建一个需要精确匹配的字符串。 codecademy JavaScript课程是为完整的新手所以他们寻找的答案是使用for循环和广泛的if/else陈述来给我们实践。

从一个学习曲线点扩展版本:

/*jshint multistr:true */ 
var text = "The video provides a powerful way to help you prove \ 
your point. When you click Online video, you can paste in the \ 
embed code for the video you want to add. You can also type a \ 
keyword to search online for the video that best fits your document."; 

var theWord = "video"; 
var hits = []; 

for (var i = 0; i < text.length; i++) { 
    if (theWord === text.substr(i,theWord.length)) { 
     hits.push(i); 
     i += theWord.length-1; 
    } 
} 

if (hits.length) { 
    for (i = 0; i < hits.length; i++) { 
     console.log(hits[i],text.substr(hits[i],theWord.length)); 
    } 

} else { 
    console.log("No matches found."); 
} 

 

再有就是通过@Sanda提到match()这是好事,知道现实生活中的实例:

/*jshint multistr:true */ 

var text = "The video provides a powerful way to help you prove \ 
your point. When you click Online video, you can paste in the \ 
embed code for the video you want to add. You can also type a \ 
keyword to search online for the video that best fits your document."; 

var hits = text.match(/video/g); 


if (hits.length === 0) { 
    console.log("No matches found."); 
} else { 

    console.log(hits); 
} 

 

我希望这有助于codecademy民间!

0

这里是SUBSTR的解决方案:

/*jshint multistr:true */ 

var text = "Lampe hinab la Samir licht schen er te recht. \ 
     Furchtete verodeten wo te Song flanierte \ 
     grundlich er Samir he. Bekam einem blank \ 
     da so schlo mu so.", 
    myName = "Samir", 
    hits = []; 

for (var i = 0; i < text.length; i++) { 
    if (text[i] === myName[0]) { 
     var substr = text.substr(i, myName.length); 
     if (substr === myName) { 
      for (var j = i; j < i + myName.length; j++) { 
       hits.push(text[j]); 
      } 
     } 
    } 
} 
if (hits.length === 0) { 
    console.log ("Your name wasn't found!"); 
} else { 
    console.log (hits); 
}; 
1

也遇到了这个演习代号学院的JavaScript基础课程。

我的解决方案如下。非常基本,但是完成了这项工作。

var text = "There is a real risk of the British economy being harmed Olly which has the potential to create an environment in which businesses could go under and Olly jobs could be lost. The economic Oggle uncertainty was tangible yesterday as global financial markets lost Olly up to $2 Trillion USD value and the pound was at it’s lowest value for 30 years before recovering somewhat." 

var myName = "Olly" 
var hits = [] 

for (var i = 0; i < text.length; i++) { 
    if (text[i] === "O" && text[i +1] === "l" && text [i +2] === "l" &&  text[i + 3] === "y") { 
     for(var j = i; j < (myName.length + i); j++) { 
      hits.push(text[j]); 
     } 
    } 
} 

if (hits.length === 0) { 
    console.log("Your name wasn't found!"); 
} else { 
    console.log(hits); 
}