Javascript 如何在JS中使用正则表达式获取字体族的子字符串

Javascript 如何在JS中使用正则表达式获取字体族的子字符串,javascript,regex,string,substring,font-family,Javascript,Regex,String,Substring,Font Family,我试图从表示html部分的字符串中获取字体系列。字体族可以是任何字体,这就是为什么我考虑使用正则表达式。字符串也可以是x个字符。输入的一个例子是: '<body id="foo" class="bar"><span style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas">Example Text</span><

我试图从表示html部分的字符串中获取字体系列。字体族可以是任何字体,这就是为什么我考虑使用正则表达式。字符串也可以是x个字符。输入的一个例子是:

'<body id="foo" class="bar"><span style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas">Example Text</span></body>'
“示例文本”
理想的结果将是“Consolas”。然而,我不太擅长正则表达式,也不能想出一个合适的正则表达式

数据以元素的字符串表示形式出现

解决方案:
感谢terrymorse(评论)和Gerardo Cabrera(公认答案)!我不知道如何将html字符串转换为元素。能够根据他们的反馈拉出字体系列

正如大家所说,使用正则表达式尝试解析HTML不是一个好主意。只需像这样使用DOM:

// Like the user   terrymorse   did
let div = document.createElement('div'); 
div.innerHTML = '<body id="foo" class="bar"><span style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas">Example Text</span></body>';

let children = div.children;

console.log(children[0].style.fontFamily);
//就像用户terrymorse那样
设div=document.createElement('div');
div.innerHTML='示例文本';
让children=div.children;
console.log(子项[0].style.fontFamily);
您也可以使用split和array函数来查找这样的属性,但是DOM选项更好

var s = '<body id="foo" class="bar"><span style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas">Example Text</span></body>';

var x = s.split(";");
var y = x.filter(e => e.includes("font-family"))[0].split('"')[0];
 
var result = y.replace("font-family: ", "");

console.log(result)
var s='示例文本';
var x=s.split(“;”);
var y=x.filter(e=>e.includes(“字体系列”)[0].split(“”)[0];
var result=y.replace(“字体系列:,”);
console.log(结果)

即将有一大堆否决票。在他们到来之前,你可以附加你自己的尝试。你考虑过使用DOM而不是解析html字符串吗?例如
元素。style
@Evert不幸的是,数据是以字符串的形式出现的element@YevgenGorbunkov投反对票是为了什么?@Terrymose Thanks,你说得对!我不知道如何将评论标记为答案,所以我在我的问题lol中大喊了一声