Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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微调&x2B;查找名称的精确匹配项_Javascript - Fatal编程技术网

Javascript微调&x2B;查找名称的精确匹配项

Javascript微调&x2B;查找名称的精确匹配项,javascript,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'){

是否有一个内置的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);
/*jshint multistr:true*/
var text=“Sid quick brown fox跳过懒狗”;
var myName=“Sid”;
var命中率=[];
对于(变量i=0;i
无论如何,这里有一些更好的代码

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);
检查(0);
功能检查(启动){
if(text.subStr(start).indexOf(myName)>=0){
hits++;
检查(text.subStr(start.indexOf(myName));
}
}
如果(点击次数<1){
log(“找不到您的名字!”)
}
控制台日志(点击次数);
仍然没有检查它是否有效,但这是总体思路


因此,在回答“是”时,“有一个内置的Javascript字符串方法可以帮助我(您)微调此代码,以确保它只找到与名称完全匹配的代码。”

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

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);

我不知道这与其他答案是否有那么大的不同,但这就是我在不使用正则表达式的情况下,仍然用所有出现的字符串填充数组变量的方法。同样,我只使用了substring()的添加字符串方法,这使这成为另一个可能的答案。希望这有助于:

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.");
    }
var text=“blah bhlekm kdnclwi Christian blah blah blah,这篇文章中有很多废话,blahChristian又多了一个基督徒,就是这样。”;
var myName=“Christian”;
var命中率=[];

对于(i=0;i,这里有另一个解决方案,使用函数
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);
/*jshint multistr:true*/
var text=“嘿,散打怎么样?我叫艾米丽\
很高兴认识你,莎达。你的名字来自哪里,莎娜\
桑达是斯拉夫人的名字吗;
/*var myName=“散打”;
点击率=[];
对于(变量i=0;i
语法是
var=.match(/pattern/modifiers)
。我的修饰符
g
用于全局(搜索整个模式)

更多信息请点击此处:

干杯\
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);
}
废话废话废话废话废话废话废话\ 诸如此类诸如此类的胡扎伊夫,哈泽尔”; var myName=“Hafeez”; var命中率=[]; 对于(变量i=0;i
这是我自己的解决方案,使用substring()函数和search()函数,这是一个内置的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]);
    }
}
/*jshint multistr:true*/
var text=“嘿,迈克\
想和迈克、玛丽亚和安东尼斯一起踢足球吗;
var myName=“mike”;
var命中率=[];
var pos=text.search(myName);
同时(位置!=-1){
对于(var j=0;j
使用此选项仅匹配准确的名称,而不是部分名称

var text=“你好,我叫约翰尼,你是桑德拉吗,桑德拉?”;
变量名称=[“桑德拉”、“约翰”];
对于(var i=0;i”;

}
OP的示例来自codecademy,因此如果有人正在搜索与Javascript:Lesson 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);
}

还有@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);
}


我希望这有助于编解码器!以下是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);
};
/*jshint multistr:true*/
var text=“Lampe hinab la Samir licht schen er te recht\
法兰绒\
格伦德里克·萨米尔·赫本·贝卡姆一片空白\
达索·施罗·穆索。”,
myName=“Samir”,
点击率=[];
对于(变量i=0;i
在代码学院的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);
}
var text="英国经济确实存在受到损害的风险,奥利有可能创造一个环境,让企业破产,奥利的工作岗位可能流失。经济上的不确定性昨天是明显的,因为全球金融市场损失了奥利高达2万亿美元的价值,英镑处于30年来的最低值在恢复之前
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);
}