字符串方法精确文本
string method exact text
所以,我是一个全新的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
)
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 或者如果你真的想通过循环来检查字符串,那么你可以这样做。
Javascriptvar 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。
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 用Javascript重新格式化复杂文本日期字符串的更好方法
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 无法使用 $('input[name=“rate”]').focus() 聚焦文本框;方法
- 有没有一种方法可以从两个标签之间提取文本,并以我选择的格式输出
- 我能从“;输入“;事件是否有更好的方法来跟踪文本更改
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小
- 有没有一种简单的方法可以将整数转换为等效文本.即:1变为1
- 任何编写文本文件并将其替换为上一个文本文件的方法
- 使用Javascript,获取元素的方法是什么,基于打开和关闭标记之间的文本
- 如何从该文本的 onclick 方法中更改 d3.js 文本输入
- 如何在OnPaste方法中清除文本区域
- 什么'这是使用jQuery将一些文本锚定到外部链接的最简单方法
- JavaScript 表单操作更改RC方法发布文本输入提交
- 使用JavaScript过滤从网页复制的文本的最简单方法
- 有没有一种方法可以用包含ENTER键换行符的SQL数据填充文本区域表单
- Angular promise回调不是在构造函数方法内部触发,而是在对象文本方法中触发
- 我如何结合用户输入改变文本方法和一个if语句
- 在对象文本方法周围传递异步值