简体   繁体   English

Javascript / JQuery .replace()不起作用

[英]Javascript / JQuery .replace() not working

I'm using a jquery url shortener ( https://github.com/hayageek/jQuery-URL-shortener ), and I'm trying to make it so when the jquery returns the shortened url it will replace the original url in the textarea. 我正在使用一个jQuery网址缩短器( https://github.com/hayageek/jQuery-URL-shortener ),并且我试图这样做,因此当jQuery返回缩短的网址时,它将替换原始网址。 textarea的。 My problem is that the .replace() doesn't seem to be working here: 我的问题是.replace()在这里似乎不起作用:

$("#button").click(function () {
    regex = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/g ;
    var longUrlLink = $("#textarea").val().match(regex);

    jQuery.urlShortener({
        longUrl: longUrlLink,
        success: function (shortUrl) {
            $("#textarea").val().replace(longUrlLink, shortUrl);
        }
    });

});

The regex works fine, and shortUrl is is being returned fine, so the problem must lie with how I've written then last line, but I cannot seem to get it to work. 正则表达式可以正常工作,并且shortUrl可以很好地返回,因此问题必须出在我写的最后一行的方式上,但是我似乎无法使它正常工作。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

您还必须设置新值:

$("#textarea").val($("#textarea").val().replace(longUrlLink, shortUrl));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM