admin管理员组文章数量:1414908
This is part of my code that deposes a sentence into word values of an array:
//var sentence = document.forms["chatForm"]["chat"].value;
var sentence = ";Hey, this is a sentence!"; //Example
var preMsg,msg = sentence.toLowerCase().match(/[\w'-;]+/g);
var msg[0] = msg[0].replace(/^;/, '');
if (msg[0] !== preMsg[0]) { //Checks if semi-colon was removed
msg.unshift("hooray");
alert(msg[0]+" "+msg[1]); //Testing
}
What I get from JSLint:
Expected ';' and instead saw '['.
var msg[0] = msg[0].replace(/^;/, '');
The console gives me this error for the same line of code: SyntaxError: missing ; before statement
I'm just beginning to learn JavaScript, and I don't know what is wrong with that line.
This is part of my code that deposes a sentence into word values of an array:
//var sentence = document.forms["chatForm"]["chat"].value;
var sentence = ";Hey, this is a sentence!"; //Example
var preMsg,msg = sentence.toLowerCase().match(/[\w'-;]+/g);
var msg[0] = msg[0].replace(/^;/, '');
if (msg[0] !== preMsg[0]) { //Checks if semi-colon was removed
msg.unshift("hooray");
alert(msg[0]+" "+msg[1]); //Testing
}
What I get from JSLint:
Expected ';' and instead saw '['.
var msg[0] = msg[0].replace(/^;/, '');
The console gives me this error for the same line of code: SyntaxError: missing ; before statement
I'm just beginning to learn JavaScript, and I don't know what is wrong with that line.
Share Improve this question asked Dec 13, 2013 at 2:22 DimittoDimitto 832 silver badges8 bronze badges 7-
1
preMsg
is alwaysundefined
in your code.var preMsg,msg = 42;
doesn't mean both variables will have value 42. – zerkms Commented Dec 13, 2013 at 2:26 - @zerkms Is there a way to do something like that? Or do I just have to do "var preMsg = msg"? – Dimitto Commented Dec 13, 2013 at 2:32
-
[\w'-;]
probably doesn't do what you expect. It will match word character, or any ASCII character between apostrophe (x27) and semicolon (x3B), which includes mas and periods. – p.s.w.g Commented Dec 13, 2013 at 2:32 - @p.s.w.g I'm fairly sure it does. It wasn't outputting semi-colons until I added that to the list. – Dimitto Commented Dec 13, 2013 at 2:38
- @Dimitto: yep, just another explicit assignment – zerkms Commented Dec 13, 2013 at 2:40
2 Answers
Reset to default 4[
is not a valid character for a variable name, and the line
var msg[0] = msg[0].replace(/^;/, '');
is declaring a new variable named "msg[0]", you just need to remove var
and change it to:
msg[0] = msg[0].replace(/^;/, '');
You want just msg[0] = ...
var
is for declaring variables!
本文标签: javascriptSyntaxError missingbefore statementExpected 3939 and instead saw 3939Stack Overflow
版权声明:本文标题:javascript - SyntaxError: missing ; before statementExpected ';' and instead saw '[' - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745170070a2645918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论