Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

javascript regex to get tag attribute value

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

This is a very helpful site where it is easy to test regexes of different types.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...