jQuery 替换多次出现的 substring/text
jQuery replace multiple occurrences of substring/text
我目前正在尝试学习jQuery中的replace
方法。
我有一个 <div class="notes">
,其中包含以下文字
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
并想用特定值替换文本。例如,每次我看到 )(
,我都希望它换行 (<br/>
)。我试图使用 jQuery 的替换方法来实现这一点。
$(document).ready(function() {
var text = $('.notes').html().replace(")(", "<br/>");
$('.notes').html(text);
});
我注意到在执行此操作时,它只是替换了第一个实例。所以我尝试了 replaceAll
方法,尽管这对字符串没有影响。
Quick fiddle Demo 或以下代码段:
$(document).ready(function() {
var text = $('.notes').html().replace(")(", "<br/>");
$('.notes').html(text);
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
任何人都可以建议我应该怎么做吗?
给你 -
这里,/\(|\)/g
是regex(正则表达式)。标志 g
表示全局。它会导致所有匹配项被替换。
$(document).ready(function() {
var text = $('.notes').text().replace(/\(|\)/g, "<br/>");
$('.notes').html(text);
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
您需要使用全局运行的正则表达式,注意 /g
命令。
对于您的情况,您需要使用以下内容:
/\)\(/g
$(document).ready(function() {
var text = $('.notes').html().replace(/\)\(/g, "<br/>");
$('.notes').html(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
.replace()
是一个字符串方法而不是 jQuery 方法,所以一个简单的 RegExp 应该可以。
var text = $('.notes').html().replace(/\)\(/g, "<br/>");
请注意代表全局的 g
命令,这意味着它适用于所有实例。
没有正则表达式的答案(拆分和连接):
$(function() {
var notes = $('.notes');
notes.html(notes.html().split(')(').join(')<br/>('));
});
$(document).ready(function() {
$('.notes').html($('.notes').html().replace(/\)\(/g, '<br />'));
});
我目前正在尝试学习jQuery中的replace
方法。
我有一个 <div class="notes">
,其中包含以下文字
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
并想用特定值替换文本。例如,每次我看到 )(
,我都希望它换行 (<br/>
)。我试图使用 jQuery 的替换方法来实现这一点。
$(document).ready(function() {
var text = $('.notes').html().replace(")(", "<br/>");
$('.notes').html(text);
});
我注意到在执行此操作时,它只是替换了第一个实例。所以我尝试了 replaceAll
方法,尽管这对字符串没有影响。
Quick fiddle Demo 或以下代码段:
$(document).ready(function() {
var text = $('.notes').html().replace(")(", "<br/>");
$('.notes').html(text);
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
任何人都可以建议我应该怎么做吗?
给你 -
这里,/\(|\)/g
是regex(正则表达式)。标志 g
表示全局。它会导致所有匹配项被替换。
$(document).ready(function() {
var text = $('.notes').text().replace(/\(|\)/g, "<br/>");
$('.notes').html(text);
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
您需要使用全局运行的正则表达式,注意 /g
命令。
对于您的情况,您需要使用以下内容:
/\)\(/g
$(document).ready(function() {
var text = $('.notes').html().replace(/\)\(/g, "<br/>");
$('.notes').html(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
.replace()
是一个字符串方法而不是 jQuery 方法,所以一个简单的 RegExp 应该可以。
var text = $('.notes').html().replace(/\)\(/g, "<br/>");
请注意代表全局的 g
命令,这意味着它适用于所有实例。
没有正则表达式的答案(拆分和连接):
$(function() {
var notes = $('.notes');
notes.html(notes.html().split(')(').join(')<br/>('));
});
$(document).ready(function() {
$('.notes').html($('.notes').html().replace(/\)\(/g, '<br />'));
});