我尝试运行此代码,但它不起作用,有人可以帮忙吗?
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();
答案 0 :(得分:4)
以下是使用正则表达式的工作示例:
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');
答案 1 :(得分:2)
public ActionResult ThrowJsonError(Exception ex)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
Response.StatusDescription = ex.Message; // does not work
Response.StatusDescription = ex.Message.Replace('\r', ' ').Replace('\n', ' '); // works
return Json(new { Message = ex.Message }, JsonRequestBehavior.AllowGet);
}

答案 2 :(得分:0)
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.');
}
这对你有用吗?
答案 3 :(得分:0)
我会使用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]))