简体   繁体   English

为什么我的exec出现可变错误

[英]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; document.querySelector('#output-2')。innerHTML = output;

We expect to see the id = values from ag to output but the console is saying this 我们希望看到从ag到输出的id =值,但是控制台说的是

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): 您需要将变量声明移到while条件之外,并使用括号覆盖默认的运算符优先级(逻辑拍子分配):

 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) var表示新变量的初始化 ,并且此类初始化仅允许作为独立语句使用 -变量初始化不能解析为表达式,并且while(内部while(仅接受表达式(求值的东西)。)与表达式相反,一条语句会执行某些操作,例如初始化一个值,或执行一个if / then块)

Initialize match outside of the while condition instead. 而是在while条件之外初始化match

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: 您还需要在match = pattern.exec(string)表达式周围加上括号,以将其正确地与null进行比较,否则,您将为pattern.exec(string) !== null (即, booleanoutput ,这不是您想要的:

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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