[英]Match part of text inside of tag
I am in client side context. 我在客户端环境中。 I have this html: 我有这个HTML:
<p>Text text \n other text </p>
I want to match only \\n element inside paragraph, and replace only this with "br" tag. 我只想在段落内匹配\\ n元素,并仅将其替换为“ br”标记。
I want to do this only inside tag "p" and for all match. 我只想在标签“ p”内进行所有匹配。
I supposed to use regex in javascript . 我应该在javascript中使用正则表达式 。
Thanks and sorry for my bad english. 谢谢,抱歉我的英语不好。
Use html()
method with callback and inside callback replace text using String#replace
method. 将html()
方法用于回调,并在内部回调中使用String#replace
方法替换文本。
$('p').html(function(i, htm) { return htm.replace(/\\\\n/g, '<br>'); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Text text \\n other text</p>
UPDATE 1 : If it's a string then use String#replace
method. 更新1:如果是字符串,请使用String#replace
方法。
console.log( '<p>Text text \\n other text</p>'.replace(/\\n/g, '<br>') )
UPDATE 2 : If the string contains other tag element and you just want to update the p
tag then do something like. 更新2:如果字符串包含其他标签元素,而您只想更新p
标签,请执行类似操作。
var str = '<p>Text text \\n other text</p>'; console.log( // create a temporary div eleemnt $('<div>', { // set html content from string html: str }) // get all p tags .find('p') // iterate and replace \\n .html(function(i, htm) { // replace \\n with br tag return htm.replace(/\\n/g, '<br>') }) // back to the temp div .end() // get it's updated html content .html() )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
UPDATE 3 : With pure JavaScript by generating a temporary DOM element. 更新3:使用纯JavaScript通过生成临时DOM元素。
var str = '<p>Text text \\n other text</p>'; // create a temporary div element var div = document.createElement('div'); // set html content form string div.innerHTML = str; // get all p tags and convert into Array using Array.from // for older browser use [].sclice.call instead Array.from(div.getElementsByTagName('p')) // iterate over p tags .forEach(function(el) { // update the html content el.innerHTML = el.innerHTML.replace(/\\n/g, '<br>') }); // get updated html content console.log(div.innerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can use a for
loop with getElementsByTagName
: 您可以将for
循环与getElementsByTagName
:
for(i = 0; i < window.document.getElementsByTagName("p").length; i++){
window.document.getElementsByTagName("p")[i].innerHTML = window.document.getElementsByTagName("p")[i].innerHTML.replace(/\\n/g, "<br/>");
}
If this is inside a string and not inside the HTML, you can add it to a <div>
element while handling it like this: 如果这是在字符串内,而不是在HTML内,则可以在处理以下内容时将其添加到<div>
元素中:
var myString = "<p>Text text \n other text </p>";
var div = document.createElement("div");
div.innerHTML = myString;
for(i = 0; i < div.getElementsByTagName("p").length; i++){
div.getElementsByTagName("p")[i].innerHTML = div.getElementsByTagName("p")[i].innerHTML.replace(/\\n/g, "<br/>");
}
myString = div.innerHTML;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.