【问题标题】:jQuery regex not workingjQuery正则表达式不起作用
【发布时间】:2014-05-25 05:12:36
【问题描述】:

我正在尝试使用 jQuery 正则表达式替换 textarea 中的文本。但这似乎不起作用。 http://regexr.com/38t41 说我的正则表达式是有效的。但这似乎不起作用。这是 JSFiddle http://jsfiddle.net/8uzn2/9/

HTML:

<button id="updt">Update</button>
<div id="header-code-area" class="codearea">
  <textarea id="show_header" cols="88" rows="4">
    <!-- Paste this code to the header of your website -->
    <!--front--><link href='http://fonts.googleapis.com/css?family=Allerta Stencil' rel='stylesheet' type='text/css' />
  </textarea>
</div>

JQuery:

$(document).ready(function() {
    $("#updt").click(function () {
        //replace in paragraph
        var match1 = /<\!--front--><link\shref='http:\/\/fonts\.googleapis\.com\/css\?family=[\w+ \w+]+/
        console.log(match1);
        //update textarea
        $("textarea#show_header").filter(function () {
            var $this = $(this);
            $this.html(function () {
                return $this.html().replace(match1, "<!--front--><link href='http://fonts.googleapis.com/css?family=");
            });
        }); //end textarea
    });
});

【问题讨论】:

  • 你为什么在这里使用filter

标签: jquery regex textarea


【解决方案1】:

要设置表单元素的值,您应该使用val 方法。

$("#show_header").val(function (i, currentVal) {
    return currentVal.replace(match1, "<!--front--><link href='http://fonts.googleapis.com/css?family=");
});

【讨论】:

  • 谢谢,它的工作。但我不明白为什么过滤器适用于其他替代品,但它在这里不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多