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
.
如何编写regex以替换
或
与\n。我尝试将文本从div移动到textarea,但是不要让
's显示在textarea中,所以我想用\n替换。
5 个解决方案
#1
142
var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n");
or using jQuery:
或使用jQuery:
var str = $("#mydiv").html();
var regex = /<br\s*[\/]?>/gi;
$("#mydiv").html(str.replace(regex, "\n"));
例子
edit: added i
flag
编辑:添加我国旗
edit2: you can use /<br[^>]*>/gi
which will match anything between the br
and slash
if you have for example <br class="clear" />
edit2:您可以使用/ < br[^ >]* > / gi将匹配任何br和削减之间如果有例如< br类= "清晰" / >
#2
8
myString.replace(/<br ?\/?>/g, "\n")
myString。替换(/ < br \ / ?> / g,“\ n”)
#3
3
a cheap and nasty would be:
廉价而肮脏的是:
jQuery("#myDiv").html().replace("<br>", "\n").replace("<br />", "\n")
EDIT
编辑
jQuery("#myTextArea").val(
jQuery("#myDiv").html()
.replace(/\<br\>/g, "\n")
.replace(/\<br \/\>/g, "\n")
);
Also created a jsfiddle if needed: http://jsfiddle.net/2D3xx/
还在需要时创建了jsfiddle: http://jsfiddle.net/2D3xx/。
#4
3
True jQuery way if you want to change directly the DOM without messing with inner HTML:
真正的jQuery方法,如果您想直接更改DOM而不影响内部HTML:
$('#text').find('br').prepend(document.createTextNode('\n')).remove();
$(" #文本”);(br).prepend(document.createTextNode(' \ n ')).remove();
Prepend inserts inside the element, before() is the method we need here:
在元素内的Prepend插入,之前()是我们需要的方法:
$('#text').find('br').before(document.createTextNode('\n')).remove();
Code will find any <br> elements, insert raw text with new line character and then remove the <br> elements.
代码将找到任何
元素,插入原始文本和新的行字符,然后删除
元素。
This should be faster if you work with long texts since there are no string operations here.
如果您使用长文本,这应该更快,因为这里没有字符串操作。
To display the new lines:
显示新线路:
$('#text').css('white-space', 'pre-line');
#5
2
Not really anything to do with jQuery, but if you want to trim a pattern from a string, then use a regular expression:
与jQuery无关,但如果想从字符串中裁剪模式,则使用正则表达式:
<textarea id="ta0"></textarea>
<button onclick="
var ta = document.getElementById('ta0');
var text = 'some<br>text<br />to<br/>replace';
var re = /<br *\/?>/gi;
ta.value = text.replace(re, '\n');
">Add stuff to text area</button>
#1
142
var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n");
or using jQuery:
或使用jQuery:
var str = $("#mydiv").html();
var regex = /<br\s*[\/]?>/gi;
$("#mydiv").html(str.replace(regex, "\n"));
例子
edit: added i
flag
编辑:添加我国旗
edit2: you can use /<br[^>]*>/gi
which will match anything between the br
and slash
if you have for example <br class="clear" />
edit2:您可以使用/ < br[^ >]* > / gi将匹配任何br和削减之间如果有例如< br类= "清晰" / >
#2
8
myString.replace(/<br ?\/?>/g, "\n")
myString。替换(/ < br \ / ?> / g,“\ n”)
#3
3
a cheap and nasty would be:
廉价而肮脏的是:
jQuery("#myDiv").html().replace("<br>", "\n").replace("<br />", "\n")
EDIT
编辑
jQuery("#myTextArea").val(
jQuery("#myDiv").html()
.replace(/\<br\>/g, "\n")
.replace(/\<br \/\>/g, "\n")
);
Also created a jsfiddle if needed: http://jsfiddle.net/2D3xx/
还在需要时创建了jsfiddle: http://jsfiddle.net/2D3xx/。
#4
3
True jQuery way if you want to change directly the DOM without messing with inner HTML:
真正的jQuery方法,如果您想直接更改DOM而不影响内部HTML:
$('#text').find('br').prepend(document.createTextNode('\n')).remove();
$(" #文本”);(br).prepend(document.createTextNode(' \ n ')).remove();
Prepend inserts inside the element, before() is the method we need here:
在元素内的Prepend插入,之前()是我们需要的方法:
$('#text').find('br').before(document.createTextNode('\n')).remove();
Code will find any <br> elements, insert raw text with new line character and then remove the <br> elements.
代码将找到任何
元素,插入原始文本和新的行字符,然后删除
元素。
This should be faster if you work with long texts since there are no string operations here.
如果您使用长文本,这应该更快,因为这里没有字符串操作。
To display the new lines:
显示新线路:
$('#text').css('white-space', 'pre-line');
#5
2
Not really anything to do with jQuery, but if you want to trim a pattern from a string, then use a regular expression:
与jQuery无关,但如果想从字符串中裁剪模式,则使用正则表达式:
<textarea id="ta0"></textarea>
<button onclick="
var ta = document.getElementById('ta0');
var text = 'some<br>text<br />to<br/>replace';
var re = /<br *\/?>/gi;
ta.value = text.replace(re, '\n');
">Add stuff to text area</button>