javascript正则表达式以获取标签属性值 [英] javascript regex to get tag attribute value

查看:51
本文介绍了javascript正则表达式以获取标签属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的正则表达式在给定的字符串中查找 title 标记的内容:

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

接下来,我想找到 meta property ="og:title" content :

<元属性="og:title" content ="The Rock"/> 是字符串

我不知道该怎么做.我无法使用jQuery或创建任何DOM元素.它是纯字符串,我只需要处理给定的字符串

解决方案

好,没有DOM,这是正则表达式:

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

如果由于某些原因,您不想匹配字符串中的其他内容属性,则可以更加具体:

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

是一个非常有用的网站,可以轻松测试不同类型的正则表达式./p>

I am using below regex to find the content of title tag in a given string:

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

Next I want to find the content of meta property="og:title" :

<meta property="og:title" content="The Rock" /> is a string

I have no clue how to do that. I can't use jQuery or create any DOM element. Its pure a string and i have to work on a given string only

解决方案

Ok, no DOM, here is the regex:

/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.

这篇关于javascript正则表达式以获取标签属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆