本文介绍了匹配多行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用greasemonkey从Last.fm中抓取wiki数据(这不适用于他们的REST api)。我可以使用GM_xmlhttpRequest()获取页面,并且它正常返回。

I want to use greasemonkey to scrape wiki data from Last.fm (this is not possible with their REST api). I can grab the page fine with GM_xmlhttpRequest(), and it is returning properly.

我不想使用DOM处理器来处理整个页面,因为我只想要一个小块,所以我正在使用正则表达式。

I do not want to use a DOM processor to process the whole page, since I only want a small chunk, so I'm using regular expressions.

维基数据在页面中如下:

The wiki data is in the page like:

<div id="wiki">
description

description
...
</div>

所以我写道:

/\<div id="wiki"\>(.+)\<\/div\>/m.exec(data)[1];

当我在错误控制台中测试它时(多行被压平成一行,它的工作原理) ,但在页面上它失败并说

When I test this in error console (where the multiple lines are flattened into a single line, it works, but on the page it fails and says

Error: /\<div id="wiki"\>(.+)\<\/div\>/m.exec(data) is null
Source File: file:///home/jeff/.mozilla/firefox/x4su9596.default/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js
Line: 357

我猜多线模式不会让dor匹配新线,这就是我所期望的。我如何让它匹配包括换行符在内的任何字符?

I am guessing that multiline mode does not make dor match new lines, which is what I expected. How do I make it match any character including line breaks?

推荐答案

这个点与javascript中的换行符不匹配 - 这是js正则表达式的怪癖。

The dot doesn't match newlines in javascript -- a quirk of js's regex flavor.

[^] 应该工作(例如除了绝对没有的东西)

[^] should work instead (e.g. "Everything except absolutely nothing")

这篇关于匹配多行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:35