Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 字符串方法精确文本_Javascript_String_Methods_Exact Match - Fatal编程技术网

Javascript 字符串方法精确文本

Javascript 字符串方法精确文本,javascript,string,methods,exact-match,Javascript,String,Methods,Exact Match,所以,我是JavaScript的新手。我现在正在练习一个Codeacedemy教程,它让我创建了一个程序,在一个文本字符串中查找我的名字。但是,我意识到如果我使用一个与我相似的名字,它也会返回另一个名字。我可以使用什么方法,或者如何优化代码,使其仅与字符串中的确切名称匹配 代码如下: /*jshint multistr:true */ var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from South

所以,我是JavaScript的新手。我现在正在练习一个Codeacedemy教程,它让我创建了一个程序,在一个文本字符串中查找我的名字。但是,我意识到如果我使用一个与我相似的名字,它也会返回另一个名字。我可以使用什么方法,或者如何优化代码,使其仅与字符串中的确切名称匹配

代码如下:

/*jshint multistr:true */

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from Southern California and I love to code";
var myName = "Zachary";
var hits = [];
for (var i = 0; i < text.length; i++){
    if (text[i] == 'Z') {
        for (var j = i;j < (i + myName.length); j++) {
            hits.push(text[j]);
        }
    }
}
if (hits === 0) {
    console.log("Your name was not found!");
}
else {
    console.log(hits);
}
/*jshint multistr:true*/
var text=“你好,我叫扎卡里·索霍维奇。我是一个来自南加州的20岁小伙子,我喜欢编码”;
var myName=“Zachary”;
var命中率=[];
对于(变量i=0;i
你不需要做所有这些事情

用下面的代码找到你的名字

if(text.indexOf(myName) !== -1){
  console.log('Found');
}
如果是您希望查找的事件总数

var count = text.match(/Zachary/g).length; 
console.log(count)

你不需要做所有这些事情

用下面的代码找到你的名字

if(text.indexOf(myName) !== -1){
  console.log('Found');
}
如果是您希望查找的事件总数

var count = text.match(/Zachary/g).length; 
console.log(count)
您可以在空白处删除字符串以创建一个单词数组,然后根据测试字符串检查每个单词,从而防止子字符串中的匹配。(使用可选循环
while

Javascript

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from Southern California and I love to code",
    myName = "Zachary",
    hits = 0,
    array = text.split(/\s/),
    length = array.length,
    i = 0;

while (i < length) {
    if (myName === array[i]) {
        hits += 1;
    }

    i += 1;
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text = "Zachary Hello my name is Zachary Sohovich. I'm a 20 year old dude from ZacharySouthern California and I loZacharyve to code Zachary",
    textLength = text.length,
    myName = "Zachary",
    nameLength = myName.length,
    check = true,
    hits = 0,
    i = 0,
    j;

while (i < textLength) {
    if (check) {
        if (i !== 0) {
            i += 1;
        }

        j = 0;
        while (j < nameLength) {
            if (text.charAt(i + j) !== myName.charAt(j)) {
                break;
            }

            j += 1;
        }

        if (j === nameLength && (/\s/.test(text.charAt(i + j)) || i + j === textLength)) { 
            hits += 1;
            i += j;
        }
    }

    i += 1;
    check = /\s/.test(text.charAt(i));
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text=“你好,我叫扎卡里·索霍维奇,来自南加州,今年20岁,我喜欢编码”,
myName=“Zachary”,
点击率=0,
数组=text.split(/\s/),
length=array.length,
i=0;
while(i

或者,如果您真的想通过循环检查字符串,那么您可以这样做

Javascript

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from Southern California and I love to code",
    myName = "Zachary",
    hits = 0,
    array = text.split(/\s/),
    length = array.length,
    i = 0;

while (i < length) {
    if (myName === array[i]) {
        hits += 1;
    }

    i += 1;
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text = "Zachary Hello my name is Zachary Sohovich. I'm a 20 year old dude from ZacharySouthern California and I loZacharyve to code Zachary",
    textLength = text.length,
    myName = "Zachary",
    nameLength = myName.length,
    check = true,
    hits = 0,
    i = 0,
    j;

while (i < textLength) {
    if (check) {
        if (i !== 0) {
            i += 1;
        }

        j = 0;
        while (j < nameLength) {
            if (text.charAt(i + j) !== myName.charAt(j)) {
                break;
            }

            j += 1;
        }

        if (j === nameLength && (/\s/.test(text.charAt(i + j)) || i + j === textLength)) { 
            hits += 1;
            i += j;
        }
    }

    i += 1;
    check = /\s/.test(text.charAt(i));
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text=“Zachary你好,我叫Zachary Sohovich。我是一个20岁的小伙子,来自加利福尼亚州南部的Zachary,我喜欢编码Zachary”,
text长度=text.length,
myName=“Zachary”,
nameLength=myName.length,
检查=正确,
点击率=0,
i=0,
J
while(i

注意:还有许多其他可能的解决方案也可以这样做。

您可以在空白处编辑字符串以创建一个单词数组,然后根据测试字符串检查每个单词,从而防止子字符串中的匹配。(使用可选循环
while

Javascript

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from Southern California and I love to code",
    myName = "Zachary",
    hits = 0,
    array = text.split(/\s/),
    length = array.length,
    i = 0;

while (i < length) {
    if (myName === array[i]) {
        hits += 1;
    }

    i += 1;
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text = "Zachary Hello my name is Zachary Sohovich. I'm a 20 year old dude from ZacharySouthern California and I loZacharyve to code Zachary",
    textLength = text.length,
    myName = "Zachary",
    nameLength = myName.length,
    check = true,
    hits = 0,
    i = 0,
    j;

while (i < textLength) {
    if (check) {
        if (i !== 0) {
            i += 1;
        }

        j = 0;
        while (j < nameLength) {
            if (text.charAt(i + j) !== myName.charAt(j)) {
                break;
            }

            j += 1;
        }

        if (j === nameLength && (/\s/.test(text.charAt(i + j)) || i + j === textLength)) { 
            hits += 1;
            i += j;
        }
    }

    i += 1;
    check = /\s/.test(text.charAt(i));
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text=“你好,我叫扎卡里·索霍维奇,来自南加州,今年20岁,我喜欢编码”,
myName=“Zachary”,
点击率=0,
数组=text.split(/\s/),
length=array.length,
i=0;
while(i

或者,如果您真的想通过循环检查字符串,那么您可以这样做

Javascript

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from Southern California and I love to code",
    myName = "Zachary",
    hits = 0,
    array = text.split(/\s/),
    length = array.length,
    i = 0;

while (i < length) {
    if (myName === array[i]) {
        hits += 1;
    }

    i += 1;
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text = "Zachary Hello my name is Zachary Sohovich. I'm a 20 year old dude from ZacharySouthern California and I loZacharyve to code Zachary",
    textLength = text.length,
    myName = "Zachary",
    nameLength = myName.length,
    check = true,
    hits = 0,
    i = 0,
    j;

while (i < textLength) {
    if (check) {
        if (i !== 0) {
            i += 1;
        }

        j = 0;
        while (j < nameLength) {
            if (text.charAt(i + j) !== myName.charAt(j)) {
                break;
            }

            j += 1;
        }

        if (j === nameLength && (/\s/.test(text.charAt(i + j)) || i + j === textLength)) { 
            hits += 1;
            i += j;
        }
    }

    i += 1;
    check = /\s/.test(text.charAt(i));
}

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
var text=“Zachary你好,我叫Zachary Sohovich。我是一个20岁的小伙子,来自加利福尼亚州南部的Zachary,我喜欢编码Zachary”,
text长度=text.length,
myName=“Zachary”,
nameLength=myName.length,
检查=正确,
点击率=0,
i=0,
J
while(i


注意:还有许多其他可能的解决方案也会对您起到同样的作用。

这是我对该课程的增强版

/*jshint multistr:true */
var text = "Anastasius is known to have had a brother named Flavius Paulus, who served \
asRoman consul in 496. A sister-in-law, known as Magna, was mother to Irene and  \
mother-in-law to Olybrius. This Olybrius was son of Anicia Juliana and Areobindus \
Dagalaiphus Areobindus. The daughter of Olybrius and Irene was named Proba. She \
married Probus and was mother to a younger Juliana. This younger Juliana married another \
Anastasius and was mother of Areobindus, Placidia, and a younger Proba. Another nephew \
of Anastasius was Flavius Probus, Roman consul in 502. Caesaria, sister of Anastasius, \
married Secundinus. They were parents to Hypatius and Pompeius. Flavius Anastasius \
Paulus Probus Moschianus Probus Magnus, Roman Consul in 518 also was a great-nephew of \
Anastasius. His daughter Juliana later married Marcellus, a brother of Justin II. The \
extensive family may well have included viable candidates for the throne.";

var textAsWords = text.split(/\s/);

var myName = "Anastasius";

var hits = [];

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

if (hits === 0) {
    console.log("Your name was not found!");
} else {
    console.log(hits);
}
/*jshint multistr:true*/
var text=“众所周知,阿纳斯塔修斯有一个名叫弗拉维乌斯·保卢斯的兄弟,他曾为\
496年成为罗马领事。一位名叫麦格纳的嫂子是艾琳和艾琳的母亲\
奥里布里厄斯的岳母。这位奥里布里厄斯是安妮西亚·朱利安娜和阿雷奥宾杜斯的儿子\
达伽拉伊福斯·阿罗宾德斯。奥里布里厄斯和艾琳的女儿名叫普罗巴。她\
与普罗布斯结婚,是一个年轻的朱莉安娜的母亲。这个年轻的朱莉安娜娶了另一个\
阿纳斯塔修斯是阿雷奥宾杜斯、普拉西迪亚和一个年轻的普罗巴的母亲。另一个侄子\
阿纳斯塔修斯的祖先是弗拉维乌斯·普罗布斯,公元502年的罗马执政官。凯撒利亚,阿纳斯塔修斯的妹妹\
与塞孔迪努斯结婚。他们是海帕提乌斯和庞贝的父母。弗拉维乌斯·阿纳斯塔修斯\
保罗·普罗布斯·莫斯基亚努斯·普罗布斯·马格纳斯(Paulus Probus Moschianus Probus Magnus),公元518年的罗马领事,也是他的侄子\
阿纳斯塔修斯。他的女儿朱莉安娜后来嫁给了马塞勒斯,贾斯汀二世的兄弟\
这个庞大的家族很可能包括了王位的候选人。”;
var textAsWords=text.split(/\s/);
var myName=“Anastasius”;
var命中率=[];
for(var i=0;i