C#里所用的正则表达式,如果要提取字符串里的子匹配项(我都不知道那个叫啥名字,别名?)是很方便的,比如:
Regex rx = new Regex(@"<title>(?<title>[\s\S]+)?</title>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match m = rx.Match(content);
if (m.Success)
{
string title = m.Result("${title}");
}
- 1
- 2
- 3
- 4
- 5
- 6
但如果是javascript呢,咋整?两边语法好像稍有点不一样。我弄了下,只有一些支离破碎的经验值,先记录下来:
str = "<html><head><title>呵呵呵</title></head><body></body></html>";
reg = /<title>([\s\S]+)<\/title>/gi
var result;
if ((result = reg.exec(str)) != null) {
alert(result);//显示“<title>呵呵呵</title>”,“呵呵呵”
alert(result[1]);//显示“呵呵呵”
//
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2021.09.05
如果有多个匹配,需要用循环语句来获取
function getLevels (content) {
let levels = ''
let reg = /\d+级/gi
let ms = reg.exec(content)
while (ms != null && ms.length > 0) {
levels += '、' + ms[0]
ms = reg.exec(content)
}
return (levels.length > 0) ? levels.substr(1) : levels
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
参考文章:
http://www.html-js.com/article/3420