0

I'm kinda lost with Regex and would appreciate some help.

Target: To extract the URL between the two " ", without returning the " themselves.

Base string:

<a href="somerandomurl" class="btn btn-xs btn-default "><span class="fa fa-eye fa-fw poptip" data-toggle="tooltip" title="" data-original-title="Inspect in-game"></span></a>

I came up with the following solution:

(="(.*)" class="btn btn-xs btn-default ")

Too bad it is matching

="somerandomurl" class="btn btn-xs btn-default "

Is it possible to match only the inner result, without the delimiters?

somerandomurl

Since this should be included in a script that should run as fast as possible, maybe there is a faster and better approach? In reality this regex search will be applied on a complete website.

6
  • 4
    Best not to try to parse HTML with regex. What language? Use an HTML parser instead Commented Mar 8, 2020 at 10:30
  • 2
    Use a DOMParser with for example document.querySelectorAll("a.btn.btn-xs.btn-default"); and get the href Commented Mar 8, 2020 at 10:51
  • 1
    What language/tool are you using? From the regex tag info: "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool." Commented Mar 8, 2020 at 11:27
  • Parsing HTML with regex is a hard job HTML and regex are not good friends. Use a parser, it is simpler, faster and much more maintainable. Commented Mar 8, 2020 at 11:28
  • Look at the answers to this similar question: stackoverflow.com/questions/1454913/… Commented Mar 8, 2020 at 12:38

1 Answer 1

1

Using RegEx to match markup is usually not a good idea. If you have the option you might want prefer a HTML / DOM parser.

That said your RegEx should match the sample in most languages. But it defines two sets of parenthesis so the result you want is located in group 2. Both group 0 and 1 will hold the full match.

If you have trouble reading the correct result group, please provide some additional information like which language your're working in and preferabbly a snippet.

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

1 Comment

I used Cheerio first (a dom parser) but noticed it adds 30 ms delay/computing time, whereas regex adds only 2 ms delay/computing time. Too bad I'm so bad with its syntax :< In my usecase every ms matters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.