Regular expression to get a class name from html

前端 未结 5 1133
温柔的废话
温柔的废话 2021-01-13 09:39

I know my question might look like a duplication for this question, but its not
I am trying to match a class name inside html text that comes from the

5条回答
  •  半阙折子戏
    2021-01-13 10:25

    Test it here: https://regex101.com/r/vnOFjm/1

    regexp: (?:class|className)=(?:["']\W+\s*(?:\w+)\()?["']([^'"]+)['"]

    const regex = /(?:class|className)=(?:["']\W+\s*(?:\w+)\()?["']([^'"]+)['"]/gmi;
    const str = `
    `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

提交回复
热议问题