Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

javascript - Matching multiline Patterns

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.

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>

So I wrote:

/<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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...