2013-08-26 74 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); 
}