正则表达式匹配所有有效doctype标签的字符串

时间:2018-10-16 11:39:35

标签: regex

我需要一个可以捕获所有有效doctype标记的正则表达式字符串。 我已经尝试过/(<!doctype )html+\s*(>)?/g//(<!doctype )html+\s*(>)(.+)?/g/,但他们俩都一直没有抓住重点。 :(

const valid1 = `<!doctype html>`
const valid2 = `<!doctype html     >`
const valid3 = `<!doctype html     >
                <p></p>`
const invalid1 = `<!doctype htmlfoobar>`
const invalid2 = `<!doctype htmlfoobar>abcd`

2 个答案:

答案 0 :(得分:2)

那一个呢?

awk '/<!doctype +html *>/ { print "ok";}'
<!doctype html>
ok
<!doctype html     >
ok
<!doctype html     >
ok
                <p></p>
<!doctype htmlfoobar>
<!doctype htmlfoobar>abcd

答案 1 :(得分:1)

您可以使用

<!doctype\s+html[\s>]

详细信息

  • <!doctype-文字字符串
  • \s+-1个以上空格 -html-文字字符串
  • [\s>]-空格或>

请参见regex demo

相关问题