jQuery javascript regex Replace
\n과 함께
대체할 regex를 작성하려면 어떻게 해야 합니까?<br />아니면<br>와 함께\n. div에서 텍스트 영역으로 텍스트를 이동하려고 하는데 원하지 않습니다.<br>텍스트 영역에 표시하기 때문에 대신하고 싶습니다.\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깃발을 올리다
edit2: 사용할 수 있습니다./<br[^>]*>/gi어떤 것이든 일치할 것입니다br그리고.slash예를 들면<br class="clear" />
myString.replace(/<br ?\/?>/g, "\n")
내부 HTML을 방해하지 않고 DOM을 직접 변경하려면 진정한 jQuery 방식:
$('#text'. ('br')를 찾습니다.prepend(문서.createTextNode('\n').제거();
요소 내부에 삽입물을 붙이기 전에 필요한 방법은 다음과 같습니다.
$('#text').find('br').before(document.createTextNode('\n')).remove();
코드는 모든 <br> 요소를 찾고 새로운 줄 문자로 원시 텍스트를 삽입한 다음 <br> 요소를 제거합니다.
여기는 문자열 작업이 없기 때문에 긴 텍스트로 작업하면 더 빠를 것입니다.
새 선 표시하기
$('#text').css('white-space', 'pre-line');
값싸고 고약한 것은 다음과 같습니다.
jQuery("#myDiv").html().replace("<br>", "\n").replace("<br />", "\n")
편집
jQuery("#myTextArea").val(
jQuery("#myDiv").html()
.replace(/\<br\>/g, "\n")
.replace(/\<br \/\>/g, "\n")
);
필요한 경우 jsfiddle도 만들었습니다. http://jsfiddle.net/2D3xx/
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>
언급URL : https://stackoverflow.com/questions/5959415/jquery-javascript-regex-replace-br-with-n
'programing' 카테고리의 다른 글
| 해시는 파이썬에서 무엇을 합니까? (0) | 2023.10.28 |
|---|---|
| Floating action button(팹) 아이콘 크기 조정 (0) | 2023.10.28 |
| WooCommerce 체크아웃 페이지의 카트 합계에 ajax를 사용한 사용자 지정 수수료 추가 (0) | 2023.10.28 |
| 플러그인을 사용하지 않고 jQuery 기능을 완화 (0) | 2023.10.28 |
| 지정된 주소 gdb에서 찾은 표시 값 (0) | 2023.10.28 |