How to use regular expression to match a special meta tag in html string using javascript All In One

发布时间 2023-10-22 15:31:10作者: xgqfrms

How to use regular expression to match a special meta tag in html string using javascript All In One

meta tag

error ❌

const html = `
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="My Favorite Girlfriend"/>
    <meta name="twitter:site" content="@Hulu"/>
  </head>
</html>
`;

// regex groups
const result = [];
// html.match(/(^<meta name="twitter:url" content="[.]+"\/>$)/ig, (group) => {
// html.match(/(^<meta name="twitter:url" content="[\w+\s*]+"\/>$)/ig, (group) => {
// html.matchAll(/(^<meta name="twitter:url" content="([\w+\s?]+)"\/>$)/ig, (group) => {
html.matchAll(/(^<meta name="twitter:url" content="[\w+\s?]+"\/>$)/ig, (group) => {
  result.push(group);
});

console.log(`result`, result);

image

https://regexper.com/#%2F(^<meta name%3D"twitter%3Aurl" content%3D"[\w%2B\s%3F]%2B"\%2F>%24)%2Fig

solution ✅


demos

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

regex tools

  1. Regexper

https://regexper.com/#%2F^<meta name%3D"twitter%3Aurl" content%3D"([\w%2B\s%3F]%2B)"\%2F>%24%2Fig

image

  1. regex101

build, test, and debug regex

https://regex101.com/r/rX1zK7/103

元字符(Metacharacter)是拥有特殊含义的字符:

元字符 描述
. 查找单个字符,除了换行\r和行结束符\n。
\w 查找单词字符: 数字、字母及下划线。
\W 查找非单词字符。
\d 查找数字。
\D 查找非数字字符。
\s 查找空白字符。
\S 查找非空白字符。
\b 匹配单词边界。
\B 匹配非单词边界。
\0 查找 NULL 字符。
\n 查找换行符。
\f 查找换页符。
\r 查找回车符。
\t 查找制表符。
\v 查找垂直制表符。
\xxx 查找以八进制数 xxx 规定的字符。
\xdd 查找以十六进制数 dd 规定的字符。
\uxxxx 查找以十六进制数 xxxx 规定的 Unicode 字符。

https://www.runoob.com/jsref/jsref-obj-regexp.html

. / regex dot

https://www.runoob.com/jsref/jsref-regexp-dot.html

refs

https://stackoverflow.com/questions/77338957/how-to-use-regular-expression-to-match-a-special-meta-tag-in-html-string-using-j#

https://www.cnblogs.com/xgqfrms/p/17780326.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!