admin管理员组文章数量:1241159
I am trying to make an app that counts how many times each character occurs in a given string. So for example, in the string "hello 12355" it should match with all the numbers past 1, and not with the "hello" part. However, when I try to run the code, I get this in the console:
"Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat at new RegExp ()"
When I change xy to anything past 92, however, the code runs fine. From what I've read about this error, it means that you have to double backslash some characters because they mean something in Regexp. However, I can't double backslash the Unicode variable without affecting all of the Unicode values. Can anyone help?
Here is my code:
var occArray = [];
var occChars = [];
var xy = 50;
for (i = xy; i < 100; i++) {
var unicodeChar = String.fromCharCode(i);
var counter = new RegExp(unicodeChar, 'g');
var occurence = "hello 12355";
var occ = (occurence.match(counter) || []).length;
occArray.push(occ);
occChars.push(unicodeChar);
}
alert(occArray);
alert(occChars);
I am trying to make an app that counts how many times each character occurs in a given string. So for example, in the string "hello 12355" it should match with all the numbers past 1, and not with the "hello" part. However, when I try to run the code, I get this in the console:
"Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat at new RegExp ()"
When I change xy to anything past 92, however, the code runs fine. From what I've read about this error, it means that you have to double backslash some characters because they mean something in Regexp. However, I can't double backslash the Unicode variable without affecting all of the Unicode values. Can anyone help?
Here is my code:
var occArray = [];
var occChars = [];
var xy = 50;
for (i = xy; i < 100; i++) {
var unicodeChar = String.fromCharCode(i);
var counter = new RegExp(unicodeChar, 'g');
var occurence = "hello 12355";
var occ = (occurence.match(counter) || []).length;
occArray.push(occ);
occChars.push(unicodeChar);
}
alert(occArray);
alert(occChars);
Share
Improve this question
edited Apr 2, 2017 at 17:58
Mister_Maybe
asked Apr 2, 2017 at 17:36
Mister_MaybeMister_Maybe
1471 gold badge1 silver badge15 bronze badges
1
- Possible duplicate of JavaScript regular expression "Nothing to repeat" error – Muhammad Reza Irvanda Commented Apr 2, 2017 at 17:47
1 Answer
Reset to default 11I think it happens because some character you pass for unicodeChar
is special character in Regex(In your error, it's ?
which is 63). Please consider to detect whether it's special character or not, if it is, you can add backslash infront of it before you pass it to Regex.
May this helps you.
本文标签: javascriptInvalid Regular Expression Nothing to Repeat ErrorStack Overflow
版权声明:本文标题:javascript - Invalid Regular Expression: Nothing to Repeat Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740088682a2223883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论