Javascript/RegExp: Lookbehind Assertion is causing a “Invalid group” error

前端 未结 3 1434
栀梦
栀梦 2020-12-10 05:54

I\'m doing a simple Lookbehind Assertion to get a segment of the URL (example below) but instead of getting the match I get the following error:

Uncaught Syn         


        
相关标签:
3条回答
  • 2020-12-10 06:00

    Also you could use String.prototype.match() instead of RegExp.prototype.exec() in the case of global(/g) or sticky flags(/s) are not set.

    var regex = /\#\!\/([^\/]+)/;
    var url = "http://my.domain.com/index.php/#!/write-stuff/something-else";
    var match = url.match(regex); // ["#!/write-stuff", "write-stuff", index: 31, etc.,]
    console.log(match[1]); // "write-stuff"
    
    0 讨论(0)
  • 2020-12-10 06:20

    Javascript doesn't support look-behind syntax, so the (?<=) is what's causing the invalidity error. However, you can mimick it with various techniques: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

    0 讨论(0)
  • 2020-12-10 06:25

    I believe JavaScript does not support positive lookbehind. You will have to do something more like this:

    <script>
    var regex = /\#\!\/([^\/]+)/;
    var url = "http://my.domain.com/index.php/#!/write-stuff/something-else";
    var match = regex.exec(url);
    alert(match[1]);
    </script>
    
    0 讨论(0)
提交回复
热议问题