javascript重定向和替换

<script type = "text/javascript">
    var r = window.location.href;
    if (r.match(/#|%23/)) {
        r = r.replace(/#|%23/gi, \"@NUM@\");
        window.location.href = r;
    }
</script>

This script will redirect the page and replace # symbols with @NUM@ in the URL (don't ask why)...

I'm trying to modify the script so it doesn't replace "#" if it is at the end of the URL

example:

http://www.example.com/test.php?f=abc#def

will become

http://www.example.com/test.php?f=abc@NUM@def

But this link:

http://www.example.com/test.php?f=abc#def#

will become:

http://www.example.com/test.php?f=abc@NUM@def#

Use a positive lookahead to guarantee that there is at least one more character:

/(#|%23)(?=.)/g