Search code examples
javascripthtmlgetregular-language

Retrieve a value by Javascript Regular


I have :

<script>
var s="/xyz/abc/5-6-12-0-0-1-0";
document.write(s.match(/((\d+)$)/g));
</script>

I've tried everything I know but I can not figure out how to get reasonable results "1". It is always the last number 0

I need to get the value "1" by regular.

This demo : http://jsfiddle.net/51ezkxaq/


Solution

  • You can positive look ahead assertion for finding the digit before the last

    var s = "/xyz/abc/5-1-12-0-0-1-0";
    document.write(s.match(/(\d+)(?=-\d+$)/));

    Regex explanation

    Regular expression visualization