我试着运行这个代码,但它不工作,能有人帮忙吗?
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');
function checkFirstLetterOfLastName(lastname) {
if((/^[A-L].+/i).test(lastname)) {
console.log('starts with A-L');
}
else
{
console.log('does not starts with A-L');
}
}
checkFirstLetterOfLastName("hello")
foo('Avery');
foo('David');
foo('Laura');
foo('Michael');
foo('Zachary');
function foo(x) {
if(x.match(/^[A-L]/i)) {
console.log('Go stand in first line.')
}
else console.log('Go stand in second line.');
}
这个对你有用吗?
我将使用RegEx并使用expression.test
方法,如下所示:
// a string that starts with a letter between A and L
var str = 'Hello!'
// a string that does not start with a letter between A and L
var notPass = 'SHould not pass'
// Note: this only checks for capital letters
var expr = /[A-L]/
console.log(expr.test(str[0]))
console.log(expr.test(notPass[0]))