checkFirstLetterOfLastName

checkFirstLetterOfLastName

我尝试运行此代码,但无法正常工作,有人可以帮忙吗?

var lastName = document.queryselector('lastName');
var message = document.queryselector('message');

function checkFirstLetterOfLastName() {
 if (/^[A-L]/.test(lastName)) {
 message.textContent = 'Go stand in first line';
 } else {
 message.textContent = 'Go stand in first line';
 }
}

checkFirstLetterOfLastName();

最佳答案

这是一个使用正则表达式的工作示例:



function checkFirstLetterOfLastName(lastName) {
  if (/^[A-L]/.test(lastName)) {
    console.log(lastName, 'starts with A-L');
  } else {
    console.log(lastName, 'does not start with A-L');
  }
}

checkFirstLetterOfLastName('Carlson');
checkFirstLetterOfLastName('Mathews');

关于javascript - 如果比较姓氏以字母A-L开头的if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39777960/

10-10 13:50