jQuery javascript regex替换< br>与\ n
问题描述:
如何编写正则表达式以替换< br />
或< br>
code> \\\
。我正在尝试将文本从div移动到textarea,但不希望在textarea中显示< br>
,所以我想用 \ n 。
How do i write a regex to replace <br />
or <br>
with \n
. I'm trying to move text from div to textarea, but don't want <br>
's to show in the textarea, so i want to replace then with \n
.
答
var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n");
或使用jQuery:
var str = $("#mydiv").html();
var regex = /<br\s*[\/]?>/gi;
$("#mydiv").html(str.replace(regex, "\n"));
编辑已添加 i
flag
edit2:您可以使用 /< br [^>] *> / gi
如果您有例如< br class,它将匹配
br
和斜杠
之间的任何内容=清除/>
edit2: you can use /<br[^>]*>/gi
which will match anything between the br
and slash
if you have for example <br class="clear" />