sukkis sukkis - 2 years ago 94
Javascript Question

How to javascript match exactly 7 numbers from different urls

I have URL addresses like

My javascript bookmarklet looks something like below.

javascript:(function(){javascript:var location_pathname = document.location.href;var ggId = location_pathname.match(/^[0-9]{7}$/)[1];'http://localhost/script.php?id='+ggId, '_blank')})()

How to match those 7 numbers from each urls? Above is my script but it does not work. If I match example with
code works and opens new tab http://localhost/script.php?id=9303033 but it works only in some cases.

Answer Source

If there are exactly 7 digits after slash (/) and another slash or EOL then correct RegEx is

var re = /\/(\d{7})\/?|$/; //note (\d{7}) This is what will be captured
''.match(re)[1]; //4303013
''.match(re)[1]; //4303013
''.match(re)[1]; //undefined
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download