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!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170080a4614869.html
评论列表(0条)