由于某种原因,string.split无法正常工作

时间:2020-06-02 05:58:54

标签: javascript arrays node.js

我正在尝试通过将字符串拆分为一个数组并调出下一个可用单词来打印人“ Michael”的姓氏。

运行此代码时,我不确定。我尝试使用Pythontutor来查看我在做什么错,结果发现string.split()无法正常工作。

能否请您看看并帮助我弄清楚我在做什么错?

这是我的代码:

function getMichaelLastName(inputText) {
  var names = inputText;
  var newN = [names.split(" ")]; 
  for (var i = 0; i<newN.length; i++) {
    if(newN[i] == "Michael") {
      var Michael = newN[i++];
    }
    return Michael;
  }
}
console.log(
getMichaelLastName("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?")
)

5 个答案:

答案 0 :(得分:4)

您的代码有多个问题:

  • 请勿使用newN[i++],因为您要更新i的值。请改用newN[i+1]
  • 您遇到了一些示波器问题。您可以在此处了解更多信息:https://developer.mozilla.org/en-US/docs/Glossary/Scope
  • 如果句子的最后一个单词是“ Michael”,则会出现溢出错误(newN[i+1]不存在)。
  • split函数已经返回一个数组。

function getMichaelLastName(inputText) {
  var names = inputText;
  var newN = names.split(" ");
  var michaelLastnames = []
  
  for (var i = 0; i<newN.length - 1; i++) {
    if(newN[i] == "Michael") {
      michaelLastnames.push(newN[i+1]);
    }
  }
  
  return michaelLastnames;
}

var michaelLastname = getMichaelLastName("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?")

console.log(michaelLastname)

答案 1 :(得分:1)

在迈克尔之后回话

function getMichaelLastName(inputText) {
  let newArr = []
  var names = inputText;
  var newN = names.split(" "); 
  for (var i = 0; i<newN.length; i++) {
    if(newN[i] === "Michael") {
      newArr = [...newArr, newN[i + 1]]
    }
  }
  return newArr
}

如果您想返回姓氏:

function getMichaelLastName(inputText) {
  let newArr = ''
  var names = inputText;
  var newN = names.split(" "); 
  for (var i = 0; i<newN.length; i++) {
    if(newN[i] === "Michael") {
      newArr = newN[i + 1]
    }
  }
  return newArr
}

答案 2 :(得分:1)

正则表达式。搜索单词边界,然后搜索Michael和字母字母单词。

交互式RegEx演示,其解释如下:https://regex101.com/r/gC8tsi/2
/\bMichael ([A-Z][a-z]*?)\b/g是一个正则表达式文字(/括起来的语法表示一个正则表达式)。正则表达式是一种模式语法,旨在处理带有模式的文本。
\b寻找单词边界。基本上是找到非单词元素的边界。
Michael_只是在寻找那个字符串。
[A-Z]寻找大写字母
[a-z]*?是小写字母的惰性序列(匹配到下一个单词边界\b为止)

地图和切片的末尾只是从所有找到的名称中切出名字。
||[]仅在找不到匹配项的情况下返回空数组。

function getMichaelLastName(inputText) {
  return (inputText.match(/\bMichael ([A-Z][a-z]*?)\b/g)||[]).map(x=>x.slice('Michael '.length))
}
console.log(
getMichaelLastName("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?")
)

function getLastNames(inputText, name='Michael') {
  const rex = new RegExp(String.raw`\b${name} ([A-Z][a-z]*?)\b`,'g')
  return (inputText.match(rex)||[]).map(x=>x.slice(name.length+1))
}
console.log(
getLastNames("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?",'John'),
getLastNames("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?",'Michael')
)

答案 3 :(得分:1)

此代码使用正则表达式并捕获cacheKeyWillBeUsed之后的单词(即潜在的姓氏)

Michael<space>

答案 4 :(得分:1)

function getMichaelLastName(inputText) {
    var Michaels = [];
    var names = inputText;
    var newN = names.split(' ');
    for (var i = 0; i < newN.length; i++) {
        const x = newN[i];
        if (x.includes( "Michael")) {
            if(newN[i+1].length>3)
                Michaels.push(newN[i + 1].replace('?',''));
        }

    }
    return Michaels;
}
console.log(
getMichaelLastName("Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?")
)