jQuery to identify URL and append parameters

后端 未结 1 702
离开以前
离开以前 2020-12-19 19:15

This jQuery code will run as a bookmarklet in the wordpress editor. The bookmarklet / jQuery code will edit the text in the content field and append a parameter (ex. ?parame

1条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 19:23

    First, the Fiddle.

    Your example code was doing exactly what you asked it to. It replaced the 'domain.com' portion of the the string with the domain and your parameters. Once it found 'domain.com' it stopped looking to see if there was anything else attached to it.

    Instead of a straight text replace, try using a regular expression to find URLs within a string.

    Source:

    Javascript:

    var url = 'domain.com';
    var append = '?parameter ';
    
    $(".wp-editor-area").each(function() {
        $(this).text(urlify($(this).text()));
    });
    
    function urlify(text) {
        var urlRegex = /(\b(https?|ftp|file):\/\/[domain.com][-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
        return text.replace(urlRegex, function(url) {
            return url + append;
        })
    }​
    

    0 讨论(0)
提交回复
热议问题