简体   繁体   English

javascript正则表达式获取标签属性值

[英]javascript regex to get tag attribute value

I am using below regex to find the content of title tag in a given string:我正在使用下面的正则表达式来查找给定字符串title标签的内容

alert("<title  >kjkj</title><title>jjjjj</title>".match(/<title[^>]*>([^<]+)<\/title>/)[1]);

Next I want to find the content of meta property="og:title" :接下来,我想找到的contentmeta property="og:title"

<meta property="og:title" content="The Rock" /> is a string <meta property="og:title" content="The Rock" />是一个字符串

I have no clue how to do that.我不知道该怎么做。 I can't use jQuery or create any DOM element.我不能使用 jQuery 或创建任何 DOM 元素。 Its pure a string and i have to work on a given string only它纯粹是一个字符串,我只需要处理给定的字符串

Ok, no DOM, here is the regex:好的,没有 DOM,这是正则表达式:

/content\\=\\"([A-Za-z0-9 _]*)\\"/

And if for some reason there are other content attributes in the string that you don't want to match you can just be more specific:如果由于某种原因在字符串中还有其他内容属性您不想匹配,您可以更具体:

/meta\\sproperty\\=\\"og\\:title\\"\\scontent\\=\\"([A-Za-z0-9 _]*)\\"/

This is a very helpful site where it is easy to test regexes of different types.是一个非常有用的站点,可以轻松测试不同类型的正则表达式。

While it is possible, and generally suggested, to get attributes without using regex, I've created one that will attempt to pull all attributes from an html tag string.虽然有可能并且通常建议在不使用正则表达式的情况下获取属性,但我创建了一个尝试从 html 标记字符串中提取所有属性的方法。

var string = '<a href="next.html" title="\'Next\' >>" target="_self" onclick="var target=\'_blank\'; window.open(this.href + \"?test=1\", target); return false;">Next ></a>';
var regex = new RegExp('[\\s\\r\\t\\n]*([a-z0-9\\-_]+)[\\s\\r\\t\\n]*=[\\s\\r\\t\\n]*([\'"])((?:\\\\\\2|(?!\\2).)*)\\2', 'ig');
var attributes = {};
while ((match = regex.exec(string))) {
    attributes[match[1]] = match[3];
}

Outputs:输出:

{
    href: "next.html",
    onclick: "var target='_blank'; window.open(this.href + \"?test=1\", target); return false;",
    target: "_self",
    title: "'Next' >>"
}

It works by expecting an equals sign that follows letters, numbers, underscores and hyphens, and is also followed by either a quotation mark or apostrophe.它的工作原理是期望等号跟在字母、数字、下划线和连字符之后,并且还跟有引号或撇号。 It'll use the match for the apostrophe/quotation mark to determine when the attribute ends, in case the attribute also contains other apostrophe or quotation marks.它将使用撇号/引号的匹配来确定属性何时结束,以防属性还包含其他撇号或引号。

I've tried to account for possible line breaks and spacing, however, I've still found some edge cases where it will have issues where the attribute contains an equals signs. 我试图考虑可能的换行符和间距,但是,我仍然发现了一些边缘情况,其中属性包含等号时会出现问题。

EDIT编辑

Adjusted the above code to double escape whitespace and lines, as well as fix issues with nested equals signs = and escaped quotes \\" and apostrophes \\' .调整上述代码以双重转义空格和行,并修复嵌套等号=和转义引号\\"和撇号\\'

You can get title tag text and meta tag attributes simply using this Jquery.您只需使用此 Jquery 即可获取标题标签文本和元标签属性。

var title = $('title').text();
var meta_property = $('meta').attr('property');

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

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