字符串方法精确文本

string method exact text

本文关键字:文本 方法 字符串      更新时间:2023-09-26

所以,我是一个全新的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);
}

你可以字符串。在空格处分割字符串以创建单词数组,然后根据测试字符串检查每个单词,从而防止在子字符串中进行匹配。(带备选循环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);
}
在jsfiddle

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

Javascript

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);
}
在jsfiddle

注意:有许多其他可能的解决方案可以为您做同样的事情。

**

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

它会做……简单的。

你不需要做所有这些事情。

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

if(text.indexOf(myName) !== -1){
  console.log('Found');
}

如果是出现的总次数,你想找到

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

这是我对这一课的强化版。

/*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 = "olleboleYntke Mchael MichaetMichael S1knol E E rin oef goblinMichael kdmS3ksMichael K  elkekeerifdlkùYnyslght MichaelSerind";
myName = "Michael";
var hits = [];
var getCharName = function(namePos) {
  charName = myName[namePos];
};
for (i = 0; i <= text.length; i++) {
  namePos = 0;
  getCharName(namePos);
  if (text[i] === charName) {
    var letterMatch = false;
    for (j = 0; j < myName.length; j++) {
      getCharName((namePos + j));
      if (text[(i + j)] === charName) {
        letterMatch = true;
      } else {
        letterMatch = false;
      }
    }
    if (letterMatch === true) {
      for (j = 0; j < myName.length; j++) {
        hits.push(text[(i + j)]);
      }
    }
  }
}
if (hits === 0) {
  console.log("Your name was not found!");
} else {
  console.log(hits);
}

我也在做同样的练习,这就是我的答案。

方法首先查找与myName的第一个字母匹配的对象。当我们找到匹配项时,我们在其上放置一个X标记,并运行文本以查看是否存在完全匹配项。如果完全匹配,则返回X标记,并将正确的文本长度放入输出数组"hits"中。将所有字母放入数组后,返回到X标记处继续处理其余的文本。

如果没有完全匹配,我们返回到X点并继续寻找与myName的第一个字母匹配的位置。

var text = "HahahnhahahahnhaHahahahahahahahahahahahaHahahahahahahahahahaHahahahahahnhaHahnhahahahahahahahahaHahaha"
var myName = "Hahn"
var hits =[] //the output will be an array
for(i=0; i<text.length; i++){ 
    var m = 0; 
    if(text[i] === myName[0]){ 
        for (j=0; j<myName.length; j++){ 
            if (text[i+j] !== myName[m]){ 
                break
            }
            m++; 
            if (m === myName.length){
                for (n=0; n<myName.length; n++){
                    hits.push(text[i+n]);
                }
            }
        }
    }
}
console.log(hits)

查找您的名字的最短方法是使用下面的.match示例:

var text = "Hello my name is Zachary Sohovich. I'm a 20 year old dude from 
Southern California and I love to code";
var nameCheck = text.match(/zachary/gi)
console.log(nameCheck)

注意,在你的名字后面的gi告诉控制台记录所有的匹配,不管使用什么情况。您还可以使用.match来区分大小写,将gi替换为g

相关文章: