【发布时间】: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?