[英]search name in text (javascript)
任何幫助將不勝感激:在確定自己做錯了什么之前,我無法繼續前進。
我正在使用for
循環在某些文本中搜索名稱,然后嘗試將這些字母推入數組。 由於某種原因,它沒有推動,而且我不確定我在做什么錯..還是一個新手.. thx尋求幫助!
var text = "Code it out bro, it will just get Zane Zane Zane better
from here, especially after your familiar with the various
frameworks..yeah man...";
var hits = [ ];
var nameZane = "Zane";
for (i=0; i < text.length; i++)
if (i === "Z"){
for (j=i; j < nameZane.length + i; j++){
hits.push(text[j])}
}
if (hits.length === 0){
console.log("Couldn't find " + nameZane)}
else{
console.log(hits)}
是不是i
的一個text[i]
if條件這樣使用if (text[i] === "Z") {
var text = "Code it out bro, it will just get Zane Zane Zane better from here, especially after your familiar with the variousframeworks..yeah man..."; var hits = []; var nameZane = "Zane"; for (i = 0; i < text.length; i++) if (text[i] === "Z") { for (j = i; j < nameZane.length + i; j++) { hits.push(text[j]) } } if (hits.length == 0) { console.log("What the f*** man?") } else { console.log(hits) }
編輯您的代碼:
if (i === "Z")
if (text.charAt(i) == 'Z')
和
hits.push(text[j])
至
hits.push(text.charAt(j));
var re = /Zane/g,
str = "Code it out bro, it will just get Zane Zane Zane better from here, especially after your familiar with the various frameworks..yeah man...";
while ((match = re.exec(str)) != null) {
console.log("match found at " + match.index);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.