admin管理员组文章数量:1333172
Error: Expected a conditional expression and instead saw an assignment. (no-cond-assign
)
const re = /<%([^%>]+)?%>/g;
let match;
while (match = re.exec('<%hello%> you <%!%>')) {
console.log(match);
}
Error: Expected a conditional expression and instead saw an assignment. (no-cond-assign
)
const re = /<%([^%>]+)?%>/g;
let match;
while (match = re.exec('<%hello%> you <%!%>')) {
console.log(match);
}
Doing a while
loop to reassign match, but getting no-cond-assign
error. I can still get output without errors but what is the best way to correct the syntax? Thanks
2 Answers
Reset to default 5One option is to use a do-while
loop instead, so you can break
inside a while(true)
:
const re = /<%([^%>]+)?%>/g;
while (true) {
const match = re.exec('<%hello%> you <%!%>');
if (!match) {
break;
}
console.log(match);
}
IMO, this situation is the one time in Javascript where assignment inside a conditional (in your original code) is clearer than the alternative. I wouldn't be afraid of disabling that linting rule for this one line.
Assuming you want to retrieve the first capturing group, you'll be able to use string.prototype.matchAll in modern environments:
const str = '<%hello%> you <%!%>';
const contentInsidePercents = [...str.matchAll(/<%([^%>]+)?%>/g)]
.map(match => match[1]);
console.log(contentInsidePercents);
You can simply use
while ((match = re.exec('<%hello%> you <%!%>'))!== null)
const re = /<%([^%>]+)?%>/g;
let match;
while ((match = re.exec('<%hello%> you <%!%>'))!== null) {
console.log(match);
}
本文标签: eslintJavascript While loop condition with nocondassign errorStack Overflow
版权声明:本文标题:eslint - Javascript While loop condition with no-cond-assign error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742349070a2458118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论