开发者

Problem with regexp in userscript for chrome

开发者 https://www.devze.com 2023-02-12 13:06 出处:网络
This might be a noob question, but I have tried to find an answere here and on other sites and I have still not find the answere. At least not so that I understand enough to fix the problem.

This might be a noob question, but I have tried to find an answere here and on other sites and I have still not find the answere. At least not so that I understand enough to fix the problem.

This is used in a userscript for chrome.

I'm trying to select a date from a string. The string is the innerHTML from a tag that I have managed to select. The html structure, and also the string, is something like this: (the div is the selected tag so everything within is the content of the string)

<div id="the_selected_tag">  
    <a href="http://www.something.com"  title="something xxx">link</a>  
    " 2011-02-18 23:02"  
 开发者_如何学Python   <a href="http://www.somthingelse.com" title="another link">thing</a>
</div>

If you have a solution that helps me select the date without this fuzz, it would also be great.

The javascript:

var pattern = /\"\s[\d\s:-]*\"/i;
var tag = document.querySelector('div.the_selected_tag');
var date_str = tag.innerHTML.match(pattern)[0]

When I use this script as ordinary javascript on a html document to test it, it works perfectly, but when I install it as a userscript in chrome, it doesn't find the pattern.

I can't figure out how to get around this problem.


Dump innerHTML into console. If it looks fine then start building regexp from more generic (/\d+/) to more specific ones and output everything into a console. There is a bunch of different quote characters in different encodings, many different types of dashes.

[\d\s:-]* is not a very good choice because it would match " 1", " ". I would rather write something as specific as possible:

/" \d{4}-\d{2}-\d{2} \d{2}:\d{2}"/

(Also document.querySelector('div.the_selected_tag') would return null on your sample but you probably wanted to write class instead of id)


It's much more likely that tag.innerHTML doesn't contain what you think it contains.

0

精彩评论

暂无评论...
验证码 换一张
取 消