0

I would like to create a function to analyse attributes (like data() function does) to select attributes starting with a pattern. Here is an example:

<span data-foo-bar data-foo-foo>my example<span>

I would like to select data-foo-bar and data-foo-foo because it starts with the regex data-foo-*.

I found a lot of way to use regex on attribute value but not directly on the attribute name.

Does anyone have an idea?

brcebn
  • 1,571
  • 1
  • 23
  • 46

1 Answers1

0

You can use regex

var isDataFoo = function(name) {
  return name.match(/^data\-foo/) !== null
}

Or just string indexOf

var isDataFoo = function(name) {
  return name.indexOf('data-foo') === 0
}
lipp
  • 5,586
  • 1
  • 21
  • 32