正则表达式搜索fontawesome图标名称

时间:2017-12-20 11:36:58

标签: javascript regex

需要正则表达式从字符串中搜索字体真棒图标名称。

到目前为止我尝试了什么:

function myFunction() {
    var str = '<i class="fa fa-edit"></i>'; 
    var res = str.match(/'fa-'*/);
    return res;
}

我的功能应该返回&#34; fa-edit&#34;作为图标名称

我的正则表达式应该找到什么名字?

4 个答案:

答案 0 :(得分:3)

我不知道你使用什么样的工具/语言正则表达式,但这里是一般解决方案,应该适用于大多数地方:

<i class="(?:.*\s)?([^"]*)"><\/i>

<i>标记内的最后一个类应该显示为第一个捕获组。

Demo

var str = '<i class="fa fa-edit"></i>'; 
var res = str.match(/<i class="(?:.*\s)?([^"]*)"><\/i>/);
console.log(res[1])

摘自@mike_t

的代码段

答案 1 :(得分:2)

如果我正确地假设您使用的是javascript,请尝试以下操作:

注意:我还添加了前导字符空格和单/双引号,以防类被指定为ie。 &#34; fa-edit fa&#34;或者&#39; fa-edit fa&#39;

&#13;
&#13;
var str = '<i class="fa fa-edit"></i>'; 
    var res = str.match(/.*[\ \"\']+(fa-[a-zA-z0-9]*).*/);
    console.log(res[1])
&#13;
&#13;
&#13;

答案 2 :(得分:1)

虽然正则表达式看起来很棒,但它可能遇到问题:H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

是的,我知道,如果可以使用已知的子集正则表达式;但它并不意味着应该这样做。

这个答案提供了一种将字符串转换为DOM元素的方法,获取类并为以function copy(element_id) { var aux = document.createElement("div"); aux.setAttribute("contentEditable", true); aux.innerHTML = document.getElementById(element_id).innerHTML; aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)"); document.body.appendChild(aux); aux.focus(); document.execCommand("copy"); document.body.removeChild(aux); console.log("COPY"); }开头的类解析它们

代码

&#13;
&#13;
<p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p>
  
<button onclick="copy('demo')">Copy To Clipboard Keeping Format</button>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

此正则表达式将按照标题的要求进行操作-查找惊人的图标名称。

fa-[a-zA-Z0-9-]+

示例:https://regex101.com/r/zPuxg0/3

请注意,它还将包含诸如fa-2xfa-spin等修饰符。因此,请务必根据需要将其过滤掉。

相关问题