最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex - "Nothing to repeat" - Stack Overflow

programmeradmin5浏览0评论

Here is my regex I believe is giving me trouble:

JavaScript:

theregex = /\$?\{(\d+)\}/g;

Which outputs this:

Uncaught SyntaxError: Invalid regular expression: /$?\{(\d+)\}/: Nothing to repeat.

I have read about needing more backslashes to escape the special characters - Does that change the pattern though?

I am using regex101 to help - it's telling me the above regex is valid. Clearly something thinks it isn't, and I'm not familiar enough with regular expressions to see it.

Edit: Here is the whole code block.

JavaScript:

foo: function(element, rule) {
    var message = this.defaultMessage(element, rule.method),
       theregex = /\$?\{(\d+)\}/g;

       if (typeof message === "function") {
           message = message.call(this, rule.parameters, element);
       } else if (theregex.test(message)) {
           message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
       }
       this.errorList.push({
           message: message,
           element: element
        });
        this.errorMap[element.name] = message;
        this.submitted[element.name] = message;
        }
...

Here is my regex I believe is giving me trouble:

JavaScript:

theregex = /\$?\{(\d+)\}/g;

Which outputs this:

Uncaught SyntaxError: Invalid regular expression: /$?\{(\d+)\}/: Nothing to repeat.

I have read about needing more backslashes to escape the special characters - Does that change the pattern though?

I am using regex101 to help - it's telling me the above regex is valid. Clearly something thinks it isn't, and I'm not familiar enough with regular expressions to see it.

Edit: Here is the whole code block.

JavaScript:

foo: function(element, rule) {
    var message = this.defaultMessage(element, rule.method),
       theregex = /\$?\{(\d+)\}/g;

       if (typeof message === "function") {
           message = message.call(this, rule.parameters, element);
       } else if (theregex.test(message)) {
           message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
       }
       this.errorList.push({
           message: message,
           element: element
        });
        this.errorMap[element.name] = message;
        this.submitted[element.name] = message;
        }
...
Share Improve this question edited Apr 29, 2014 at 19:00 Damon asked Apr 29, 2014 at 18:55 DamonDamon 4,54415 gold badges65 silver badges124 bronze badges 7
  • Can you show us the javascript surrounding the regex? – George Stocker Commented Apr 29, 2014 at 18:56
  • 4 It's valid in Chrome, copy/pasted from what you have above. Retype it, if you copied from somewhere else and maybe have a non-printable in there/ – Michael Berkowski Commented Apr 29, 2014 at 18:57
  • That's what I see as well. I thought maybe I more backslashes to escape the special characters or something. But then that seems to change the pattern... – Damon Commented Apr 29, 2014 at 19:09
  • Well I don't know about javascript, but can you try /[$]?\{(\d+)\}/? – Liquidpie Commented Apr 29, 2014 at 19:20
  • Forgive me asking again, I see there was an answer posted but for some reason I am unable to access it? Here is my latest regex - my latest attempt to escape the backslashes. /\$?\\\{(\d+)\\\}/g Still failing miserably. I appreciate everyone's help! – Damon Commented Apr 29, 2014 at 20:32
 |  Show 2 more ments

4 Answers 4

Reset to default 2

I have the same error and in my case is:

var nameToReplaceGloble = /nameToReplace/g;

But I solved it by concatenate string like:

var nameToReplaceGloble = "/"+nameToReplace+"/g";

I noticed you had this notation in your sample code: "{$1}", assuming you are trying to match this expression. You Regular expression should be like this instead:

\{\$?(\d+)\}

meaning:

var theregex = /\{\$?(\d+)\}/g;

Disclaimer: both this expression and the one in your sample worked fine on my end with Chrome. Please include details of what browser if you still encounter any errors.

Try to write your regex with anchors like this :

theregex = /^\$?\{(\d+)\}$/g;

Here is a detailled example: https://javascript.info/regexp-anchors

I had the same error when i used new Regexp("foo","g"), i fixed it by wrapping it in the normal way: /foo/g. But for you this is not the case, but you can try chaning this

"{$1}"

to this

"\{$1\}"
发布评论

评论列表(0)

  1. 暂无评论