最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex in chrome.declarativeContent.PageStateMatcher - Stack Overflow

programmeradmin1浏览0评论

I need to add some Regex inside my chrome.declarativeContent.PageStateMatcher.

So far I have

var matcher = new chrome.declarativeContent.PageStateMatcher({
    pageUrl: {
         urlContains: "something/someRegex/something"

    }
 });

Essentially I want to have a regular expression to evaluate to "something/4-5 char string/somithing".

How would I do this? Is this possible to do with chrome.declarativeContent.PageStateMatcher?

Thanks

I need to add some Regex inside my chrome.declarativeContent.PageStateMatcher.

So far I have

var matcher = new chrome.declarativeContent.PageStateMatcher({
    pageUrl: {
         urlContains: "something.com/someRegex/something"

    }
 });

Essentially I want to have a regular expression to evaluate to "something.com/4-5 char string/somithing".

How would I do this? Is this possible to do with chrome.declarativeContent.PageStateMatcher?

Thanks

Share Improve this question asked Mar 10, 2014 at 19:17 d9120d9120 5111 gold badge8 silver badges23 bronze badges 1
  • 1 Well, the RegEx would be: something\.com/[^/]{4,5}/something. It means any substring that isnt a "/" and is 4-5 characters in length. You also have to escape . since it is a metacharacter and means "any character". – tenub Commented Mar 10, 2014 at 19:22
Add a comment  | 

2 Answers 2

Reset to default 22

Instead of urlContains use urlMatches

This is what I'm using for both domain fedmich.com and fedche.com

chrome.runtime.onInstalled.addListener(function() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([
      {
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlMatches: '(fedmich|fedche)\.com' },
          })
        ],
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

documentation is here https://developer.chrome.com/extensions/events#property-UrlFilter-hostEquals

The regular expressions use the RE2 syntax. https://code.google.com/p/re2/wiki/Syntax

In https://developer.chrome.com/extensions/events#property-UrlFilter-hostEquals it describes urlMatches case.

发布评论

评论列表(0)

  1. 暂无评论