Find attribute value with regex

Asked

Viewed 116 times

2

I get a string for an RSS feed, the part that matters is the following:

<table feedtag="divinegoblin" ...

Each piece of the feed will always have this attribute feedtag="...". I would like to take the value of this attribute (in the divinegoblin case) with Regex. I know almost nothing of Regex, I was trying to pick up using (feedtag), but I don’t know how to pick up what is ahead (in case what is inside the quotes). How can I do?

2 answers

3


The simplest way you can use it is like this:

feedtag="([^"]*)"

This way he picks up everything inside the quotes after a feedtag=

See working: https://regex101.com/r/BwibaW/2

  • It’s not better to put "([^"]*)" to avoid marrying with a quotation mark halfway through?

  • Thank you! It worked exactly the way I wanted it to

1

Follow other alternatives to find the value, in the snippet below has an example using .substring(I know in the question you ask for regex) but I found it interesting to put, and an example using regex, returning only the value within the quotation marks through the group capture.

var a = '<table feedtxag="wesas" feedtag="divinegoblin"';
var conteudo;
conteudo = a.substring(a.indexOf('feedtag="') + 'feedtag="'.length, a.indexOf('"', a.indexOf('feedtag="') + 'feedtag="'.length));
console.log("SUBSTRING: " + conteudo);

var regexResultado = a.match(/feedtag="\s*([^"]*)\s*"/)[1];
console.log("REGEX: " + regexResultado);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.