2

I'm working on a project and I have a problem with regex, I want to test some html attributes and it works, but when I have a quotes inside this does not work.

When I add the quote I have this result.

So I hope you can help me. (I check in javascript)

/([-\$@\w\d\i]+)="([-. =\:;()'\$@\w\d\i]*)"/gim
Stephen P
  • 14,422
  • 2
  • 43
  • 67
Antharuu
  • 38
  • 3

1 Answers1

1

You can use

/([^\s=]+)="([^"\\]*(?:\\[\w\W][^"\\]*)*)"/g

See the regex demo. Details:

  • ([^\s=]+) - Group 1:
  • =" - a =" text
  • ([^"\\]*(?:\\[\w\W][^"\\]*)*) - Group 2: any zero or more chars other than " and \ and then zero or more repetitions of any escape sequence followed with any zero or more chars other than " and \
  • " - a " char.

See the JavaScript demo:

const regex = /([^\s=]+)="([^"\\]*(?:\\[\w\W][^"\\]*)*)"/g;
const str = 'title="Bonjour - \\"(le monde))\\"" style="color: red;" charset="utf-8" src="monImage.png" alt=""\ntes-t6=""';
let m, results={};
while ((m = regex.exec(str))) {
  results[m[1]]=m[2];
}
console.log(results);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563