简体   繁体   中英

Why am I getting variable errors with exec

Me and my friend are trying to resolve this but we can't figure this out we found some posts that offer solutions on this error but we don't know how to apply it to our situation so basically the code is suppose to return all regex matches which is referring to

document.querySelector('#output-2').innerHTML= output;

We expect to see the id = values from ag to output but the console is saying this

Uncaught SyntaxError: Unexpected token var

and that error is referencing to this line

while (var match = pattern.exec(string) !== null) 

so why am I getting that error?

Here is my code

 var names= document.querySelectorAll('.name'); var text = new Array(); var strtext = ''; for (var i = 0; i < names.length; i++){ var arlength = text.length; text[arlength] = '['+names[i].getAttribute('id')+']'; } strtext = text.join(' and '); document.querySelector('#output-1').innerHTML= strtext; //Show all the regex matches var string= strtext; var pattern= /\\[(.*?)\\]/ig; var output = ""; while (var match = pattern.exec(string) !== null) { output += match[1]; } document.querySelector('#output-2').innerHTML = output; 
 <p id='a' class='name'>Adam</p> <p id='b' class='name'>Bob</p> <p id='c' class='name'>Cane</p> <p id='d' class='name'>Dan</p> <p id='e' class='name'>Ed</p> <p id='f' class='name'>Fred</p> <p id='g' class='name'>Gene</p> <p id='output-1'></p> <p id='output-2'></p> 

You need to move the variable declaration outside of the while condition, and use parentheses to override default operator precedence (logical beats assignment):

 var names= document.querySelectorAll('.name'); var text = new Array(); var strtext = ''; for (var i = 0; i < names.length; i++){ var arlength = text.length; text[arlength] = '['+names[i].getAttribute('id')+']'; } strtext = text.join(' and '); document.querySelector('#output-1').innerHTML= strtext; //Show all the regex matches var string= strtext; var pattern= /\\[(.*?)\\]/ig; var output = ""; var match; while ((match = pattern.exec(string)) !== null) { output += match[1]; } document.querySelector('#output-2').innerHTML = output; 
 <p id='a' class='name'>Adam</p> <p id='b' class='name'>Bob</p> <p id='c' class='name'>Cane</p> <p id='d' class='name'>Dan</p> <p id='e' class='name'>Ed</p> <p id='f' class='name'>Fred</p> <p id='g' class='name'>Gene</p> <p id='output-1'></p> <p id='output-2'></p> 

var indicates the initialization of a new variable, and such initializations are only permitted as standalone statements - variable initialization cannot be parsed as an expression, and the inside of an while( only accepts an expression (something that evaluates to a value). (in contrast to an expression, a statement does something, like initialize a value, or carry out an if / then block)

Initialize match outside of the while condition instead.

You also need to put parentheses around the match = pattern.exec(string) expression in order to properly compare it to null , otherwise you'll be assigning the result of pattern.exec(string) !== null (that is, a boolean ) to output , which isn't what you want:

 var names= document.querySelectorAll('.name'); var text = new Array(); var strtext = ''; for (var i = 0; i < names.length; i++){ var arlength = text.length; text[arlength] = '['+names[i].getAttribute('id')+']'; } strtext = text.join(' and '); document.querySelector('#output-1').innerHTML= strtext; //Show all the regex matches var string= strtext; var pattern= /\\[(.*?)\\]/ig; var output = ""; var match; while ((match = pattern.exec(string)) !== null) { output += match[1]; } document.querySelector('#output-2').innerHTML = output; 
 <p id='a' class='name'>Adam</p> <p id='b' class='name'>Bob</p> <p id='c' class='name'>Cane</p> <p id='d' class='name'>Dan</p> <p id='e' class='name'>Ed</p> <p id='f' class='name'>Fred</p> <p id='g' class='name'>Gene</p> <p id='output-1'></p> <p id='output-2'></p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM