[英]Javascript to finding muptiple values in a string and saving to an array?
I have tried to find a way to get the values from this string:我试图找到一种方法来从这个字符串中获取值:
Edges</label><div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(226, 84, 153);"></div><div class="report_color_container"><i>Carnival Pink: </i>7</div></div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(103, 66, 48);"></div><div class="report_color_container"><i>Chocolate Brown: </i>5</div></div></div></div><div><hr><label>
I need to find a way to get the colors "Carnival Pink" and "Chocolate Brown" from this string and add them to an array.我需要找到一种方法来从这个字符串中获取颜色“嘉年华粉红”和“巧克力棕”并将它们添加到数组中。 The string is stored in a variable.
字符串存储在变量中。
I get this string above into an var with this:我把上面的这个字符串变成了一个var:
var edgesSubString = slb_report_raw.substring(
slb_report_raw.lastIndexOf("Edges"),
slb_report_raw.lastIndexOf("Corners")
);
console.log("SUB: "+edgesSubString);
Any help would be appreciated, I have exhausted my skills.任何帮助将不胜感激,我已经用尽了我的技能。
If you fix your HTML so that it can be parsed in to a jQuery object, which I've done in the following example by adding the missing leading <label>
tag, then you can find()
to target the i
elements, and then map()
to build the array of their text.如果您修复了 HTML 以便将其解析为 jQuery 对象(我在以下示例中已通过添加缺少的前导
<label>
标记完成),那么您可以使用find()
来定位i
元素,然后map()
来构建他们的文本数组。 Try this:尝试这个:
let str = '<label>Edges</label><div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(226, 84, 153);"></div><div class="report_color_container"><i>Carnival Pink: </i>7</div></div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(103, 66, 48);"></div><div class="report_color_container"><i>Chocolate Brown: </i>5</div></div></div></div><div><hr><label>'; var colours = $(str).find('i').map((i, el) => el.textContent.trim()).get(); console.log(colours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I would suggest using regular expressions.我建议使用正则表达式。
For example :例如 :
const str = 'Youre string'
const reg = /<i>[\w\s]+/g // find i tag and get all letters and spaces after that
const strings = str.match(reg).map(v => v.slice(3)) // after that remove i tag from resulting array of strings
Will output => ["Carnival Pink", "Chocolate Brown"]会输出 => ["嘉年华粉红", "巧克力棕"]
You could do this with regex你可以用正则表达式做到这一点
const STR = 'Edges</label><div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(226, 84, 153);"></div><div class="report_color_container"><i>Carnival Pink: </i>7</div></div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(103, 66, 48);"></div><div class="report_color_container"><i>Chocolate Brown: </i>5</div></div></div></div><div><hr><label>' const res = STR.match(/(?<=<i>)[^:]*/g) console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.