The nitrogen-rich polymer carbon nanoribbons probe serves as a selective photoluminescence sensor for heparin over competitor biomolecules. A stronger electrostatic attraction between metal ion (Hg2+ ion) and heparin than Hg2+ ion with oxygen/nitrogen elements is reported here. The detection of heparin with high sensitivity and selectivity. Good applicability of the sensor contributes to specific effects between Hg2+ ion and heparin.