mandaryneks
mandaryneks

Reputation: 47

Get some text from expression

I have

<meta property="og:description" content="SOME TEXT HERE">

and I would like to get only SOME TEXT HERE. I tried using:

/<meta property="og:description" content="([\s\S]+)">/

or

/^<meta property="og:description" content="([\s\S]+).">$/

but not working. Help, thanks.

Upvotes: 1

Views: 45

Answers (1)

Nicholas
Nicholas

Reputation: 137

The first regular expression you posted (and the second one with some modifications) both work for me, but can be cleaned up a little.

when you use [\s\S]+ I assume you are looking for any character at all? If so you can simply replace it with a '.+' which matches everything except for line breaks.

The following expression works for me.

^<meta property="og:description" content="(.+)">$

Here it is working with Regex 101. This makes it look like the problem is with your string or how you are applying the regex (incorrect options maybe?), not the regex itself. Make sure that the string consists of exactly what you think it does.

Edit: Note the expression I provided assumes that you will only have 1 tag per string. If you want to match multiple tags per string the following expresion will work. Tested here with Rubular.

<meta property="og:description" content="(.+?)">

Upvotes: 2

Related Questions