Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于标识URL和附加参数的jQuery_Javascript_Jquery_Regex_Wordpress_Bookmarklet - Fatal编程技术网

Javascript 用于标识URL和附加参数的jQuery

Javascript 用于标识URL和附加参数的jQuery,javascript,jquery,regex,wordpress,bookmarklet,Javascript,Jquery,Regex,Wordpress,Bookmarklet,此jQuery代码将在wordpress编辑器中作为bookmarklet运行。bookmarklet/jQuery代码将编辑内容字段中的文本,并在URL末尾附加一个参数(例如?参数) URL始终是相同的域名(例如domain.com)。但是,URL通常包含目录(例如:domain.com/directory/index.html?参数)。我需要添加jQuery代码,不管域名后面是什么 最复杂的情况是domain.com/directory/index.html?someotherparamet

此jQuery代码将在wordpress编辑器中作为bookmarklet运行。bookmarklet/jQuery代码将编辑内容字段中的文本,并在URL末尾附加一个参数(例如?参数)

URL始终是相同的域名(例如domain.com)。但是,URL通常包含目录(例如:domain.com/directory/index.html?参数)。我需要添加jQuery代码,不管域名后面是什么

最复杂的情况是
domain.com/directory/index.html?someotherparameter?parameter
。此内容区域通常包含多个url,因此脚本需要在整个文本中循环

我的半工作代码

var url= 'domain.com';
var append = ' ?parameter ';

$(".wp-editor-area").each(
    function(){
        var haystack = $(this).text();
        $(this).text(haystack.replace(url, url+ append));
    });
它正在修改的HTML代码

<div id="wp-content-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content">&lt;a title="This is a hyperlink" href="http://domain.com/directory"&gt;This is a hyperlink&lt;/a&gt;</textarea></div>
a title=“这是一个超链接”href=”http://domain.com/directory“这是一个超链接/a
其电流输出

<a title="This is a hyperlink" href="http://domain.com ?parameter /directory">This is a hyperlink</a>

请注意,我的输出不会附加在整个URL之后。如果文档正文中有更多URL,如何修改更复杂的URL并循环浏览文档

我能找到的唯一相关和类似的问题是,但我无法复制结果

谢谢

首先

您的示例代码完全按照您的要求执行。它将字符串的
'domain.com'
部分替换为域和您的参数。一旦找到了
'domain.com'
,它就停止查看是否还有其他附加内容

尝试使用查找字符串中的URL,而不是直接的文本替换

来源:

<div id="wp-content-editor-container" class="wp-editor-container">
    <textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content">
        &lt;a title="This is a hyperlink" href="http://domain.com/directory"&gt;This is a hyperlink&lt;/a&gt;
        &lt;a title="This is another hyperlink" href="http://google.com/directory"&gt;This is another hyperlink&lt;/a&gt;        
    </textarea>
</div>​
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;
    })
}​

谢谢这几乎是完美的…但是,我需要脚本只适用于特定的域。如何使脚本仅适用于domain.com而不适用于google.com?@user1512806我已更新了答案中的正则表达式,以便根据您的请求仅查找特定域的URL。如果我的答案有助于解决您的问题,那么对其进行投票并将其标记为已接受的答案将有助于其他有相同问题的人更快地找到解决方案。这很有效!非常感谢你!我很想对你的答案投赞成票,但我没有足够的声誉。但是,我将其标记为正确。再次感谢@不客气。虽然看起来你有足够的声望去投票。你试过了吗?啊,它现在起作用了!一定花了一秒钟来验证我的新代表。谢谢!