0

I'm currently learning how to use RegEx. I couldn't find a tutorial (or didnt know where to look) for replacing the result with the previous content.

I've managed to get the regex for extracting foo and bar

<div foo bar=""></div>

I want to obtain:

<div data-foo data-bar=""></div>

Here's how I'm trying to do it:

str = str.replace(/[^<div ][^/div]\w+/g, 'data-$1');

However, $1 doesnt get replaced by foo and bar.

Can someone help me? I'm stuck here!

Thank you very much!

Grozav Alex Ioan
  • 1,559
  • 3
  • 18
  • 26

1 Answers1

0

Try replaceing the regex to:

/([^<div ][^/div]\w+)/g

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references

CD..
  • 72,281
  • 25
  • 154
  • 163