开发者

jQuery - Regular Expression Problem - get href address between 3 '/' (slashes)

How can I retrieve /admin/articles/ from /admin/articles/add using regular expression and jQuery?

I already have the following jQuery code setup, but need help with Regular Expressions

<script>
    $(document).ready(function(){

        var path = "/" + location.pathname.subst开发者_Go百科ring(1);

        //Need regular expession here to make 'path' per the above mentioned example

        if ( path ) {
            $('#idlist a[href$="' + path + '"]').parent().attr('class','active');
        }
    });
</script>

Thank you all for the help.


var path = location.pathname.replace (/^(\/.+?\/.+?\/).+$/, '$1');


Turns '/admin/articles/add' into '/admin/articles/'.


If you want to extract /admin/articles/ from /admin/articles/add there is no need to use jquery or regex.

Try this.

var str ="/admin/articles/add";
var extracted = str.slice(0, str.lastIndexOf("/") + 1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜