1

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

4
  • How about $("meta[property='og:title']").attr('content') ? Commented May 24, 2015 at 6:01
  • 1
    @Ankit That'll work when it is available on page, and not only in string Commented May 24, 2015 at 6:01
  • I would appreciate if you would undo the down-vote on my answer, as I have corrected it with a valid regex that solves your problem. Commented May 24, 2015 at 6:21
  • 1
    why so much -ve votes to this question ? Commented May 24, 2015 at 6:22

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

16 Comments

i have mentioned that <meta property="og:title" content="The Rock" /> is just a string
If you have access to jQuery, which I assume you do if you're using javascript, you can use it to turn the string into an object and then access the attr property. But because you don't want to do that, here is a regex that will work.
Use wildcards *. Here is a link to example regex101.com/r/tO4jX2/3 , you will see that any possible number of spaces is covered. \s* means any number of spaces or none If you use the link I gave you, and mouse over each part of the regex it will explain it to you. It is a very useful tool for learning regex!
Ok, I'll try to break it down. Inside your regex there is a set of parenthesis (). They are wrapped around ([A-Za-z0-9 _]*) which is the part of the regex that finds "The Rock". In regex, what is inside () is called a capturing group. When you use match it returns an array that has the whole string found by the regex as the first element, and the part of the string that you "captured" with () as the second argument. So without the [1] You actually get two results (try it out). If you change [1] to [0] you will get the whole string.
@Manish see my last comment and consider that without the [1] you will get [ 'meta property="og:title" content="The Rock"', 'The Rock' ] ... you should really use console.log(...) instead of alert(...) and use the developer javascript console in your browser. That way you can keep trying things until they work instead of having to look at pop-ups all the time that don't show you the true values of what you're working with.
|
4

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.

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 \'.

Comments

-2

You can get title tag text and meta tag attributes simply using this Jquery.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.