简体   繁体   English

按br分割html,修剪它,然后添加span标签

[英]Split html by br, trim it and then add span tags

I'm trying to split lines by br tag, get the lines that start with a number and then wrap with span tags. 我正在尝试按br标签分割行,获取以数字开头的行,然后使用span标签进行换行。

  jQuery(document).ready(function($) { var brExp = /<br\\s*\\/?>/i; var swn = /^\\d/; var lines = $('.caption').html().split(brExp).filter(line => swn.test(line.trim())); jQuery.each(lines, function() { console.log(this); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> ROLLS:⠀<br> <li> ¼ cup warm water</li>⠀<br> <li>1 tablespoon active dry yeast</li>⠀<br> <li> 2 ¼ cups sharp cheddar cheese, shredded⠀</li><br> 

I've tried adding this line of code this.text(jQuery(lines).wrap('<span itemprop="recipeIngredient"></span>')); 我试过添加以下代码this.text(jQuery(lines).wrap('<span itemprop="recipeIngredient"></span>')); after console.log but it only gives an error that says cannot read property 'ownerDocument' of undefined. 在console.log之后,但仅给出错误,提示无法读取未定义的属性“ ownerDocument”。

Expected Output: 预期产量:

ROLLS:⠀<br>
<li><span itemprop="recipeIngredient">1 tablespoon active dry yeast</li><br>


<li><span itemprop="recipeIngredient">1 teaspoon sugar</span></li><br>

<li><span itemprop="recipeIngredient">2 ¼ cups sharp cheddar cheese, shredded</span></li><br>

Please help. 请帮忙。

Thanks. 谢谢。

Instead of using regular expressions you can .split("<br>"); 可以使用.split("<br>");代替使用正则表达式.split("<br>"); on the html. 在html上。 This will push each piece between br tags into an array. 这会将br标签之间的每个片段推入数组。

Then you can iterate through the array and see if the first char is a number. 然后,您可以遍历数组,查看第一个字符是否为数字。 I used jQuery's isNumeric and also took into account your fraction characters by converting them to their ASCII code and comparing against that. 我使用了jQuery的isNumeric ,还考虑了将小数字符转换为ASCII码并与之进行比较的方式。

Next just create the span to house the new lines with the proper properties and inner text and then you're done. 接下来,只需创建跨度即可容纳具有适当属性和内部文本的新行,然后完成。

 $(document).ready(function($) { let a = $("#recipe").html().split("<br>"); a.forEach(function(text) { if (text.trim().length === 0) return; let firstChar = text.trim()[0]; // char codes are for fractions ½, ¼, ¾ if ($.isNumeric(firstChar) || (firstChar.charCodeAt() >= 188 && firstChar.charCodeAt() <= 190)) { let $span = $('<span></span>').attr('itemprop', 'recipeIngredient').text(text); $("#result").append($span.prop('outerHTML') + "<br/>"); } }); }); 
 .hide { display: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="recipe" class="hide"> ROLLS:⠀<br> ¼ cup warm water⠀<br> 1 tablespoon active dry yeast⠀<br> 1 teaspoon sugar⠀<br> 1 cup milk⠀<br> 4 tablespoons butter, melted⠀<br> 1 egg, beaten⠀<br> 2 teaspoons salt⠀<br> 3 cups all-purpose flour⠀<br> 1 cup barbecue sauce⠀<br> 2 ¼ cups sharp cheddar cheese, shredded⠀<br> ½ cup green onions, chopped⠀<br> </div> <div id="result">ROLLS:<br/></div> 

Update : For your question in the comment. 更新 :对于您在评论中的问题。 If you wanted to use a list you can use jQuery to get all the li's and wrap them in a span before placing them in the result div 如果要使用列表,则可以使用jQuery获取所有li,并将它们包装在一个范围中,然后再将它们放置在结果div中

 $(document).ready(function($) { let a = $("#recipe li") a.each(function(index, elem) { let text = $(elem).text(); if (text.trim().length === 0) return; let firstChar = text.trim()[0]; // char codes are for fractions ½, ¼, ¾ if ($.isNumeric(firstChar) || (firstChar.charCodeAt() >= 188 && firstChar.charCodeAt() <= 190)) { let $span = $('<span></span>').attr('itemprop', 'recipeIngredient').text(text); $("#result").append("<li>" + $span.prop('outerHTML') + "</li>"); } }); }); 
 .hide { display: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="recipe" class="hide"> ROLLS:⠀ <ul> <li>¼ cup warm water⠀</li> <li>1 tablespoon active dry yeast⠀</li> <li>1 teaspoon sugar⠀</li> <li>1 cup milk⠀</li> <li>4 tablespoons butter, melted⠀</li> <li>1 egg, beaten⠀</li> <li>2 teaspoons salt⠀</li> <li>3 cups all-purpose flour⠀</li> <li>1 cup barbecue sauce⠀</li> <li>2 ¼ cups sharp cheddar cheese, shredded⠀</li> <li>½ cup green onions, chopped⠀</li> </div> <div>ROLLS:</div> <ul id="result"> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM