user2284570 user2284570 - 1 year ago 65
HTML Question

Regex to return all attributes of a web page that starts by a specific value

The question is simple, I need to get the value of all attributes whose value starts with
. For example, if a page contains

<iframe src="">
<meta twitter="">

Then I should get an array/list with 2 member :
(the order doesn’t matter).

I don’t want the elements, just the attribute’s value.

Basically I need the regex that returns strings starting with
and ending with a space.

Answer Source

A regular expression would likely look like this:


Make sure to escape each / and ? with a backslash. \S+ yields all subsequent non-space characters. You can also try [^\s"]+ instead of \S if you also want to exclude quote marks.

In my experience, though, regexes are usually slower than working on already parsed objects directly, so I’d recommend you try these Array and DOM functions instead:

Get all elements, map them to their attributes and filter those that start with, reduce all attributes lists to one Array and map those attributes to their values.

  .map(elem => Object.values(elem.attributes)
  .filter(attr => attr.value.startsWith("")))
  .reduce((list, attrList) => list.concat(attrList), [])
  .map(attr => attr.value);

You can find polyfills for ES6 and ES5 functions and can use Babel or related tools to convert the code to ES5 (or replace the arrow functions by hand).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download