Custom Regex Placeholders

This documentation site is deprecated. Please visit the control panel at https://monitorss.xyz for all up-to-date functionality

Regular Expressions (regex) is used to find, modify, and replace content. Suppose you want to remove/modify specific text. Or you want to remove some copyright text or whatever. Regex can do this.

All regex is applied after the text is internally sanitized/filtered, and before it is truncated (if necessary). The content you see through a test message (with [Filters Passed]: Yes) is the most accurate depiction of what your regex will be working on (content might be truncated in the test however).

Recommendation

Before applying any regexOps, you should first test your expressions and replacements at https://regex101.com/. Brute-testing regex can be quite frustrating without trying it first.

Usage/Syntax

Regex cannot be specified via Discord commands - you will have to modify your feed JSON to do so. Each regexOp object has a name key, where you will then refer to it in Discord as {placeholder:myName} where myName would be the value of the key. The placeholder can be any valid placeholder found in the command output of test for a feed. Ex: {title:SpecialName}.

The object "regexOps" is added to a feed object, and the outline is this:

Feed
{
    "url": "whatever.com",
    "channel": "1234",
    "regexOps": {
      "placeholder": [{
        "name": "whatever",
        "search": {
            "flags": "gi",
            "regex": "(?:\r\n|\r|\n)",
            "match": 0,
            "group": 0
        },
        "replacement": ""
      }]
    }
}
  • Required "placeholder" - Replace placeholder with any placeholder such as description, title, etc..

  • Required "name" - Reference to what the custom placeholder will be named. If there is no name, the regexOp in question will be ignored. For example in the snippet above, it would be referred to as {placeholder:whatever}.

  • Required "search" - The value should be an object of the following keys:

    • Required "regex" - Your regex expression.

    • Optional "match" - Match number (not string) from your regex expression. Defaults to 0. Note that the first match is 0 and not 1.

    • Optional "group" - Group number (not string) of the selected match from your regex expression. Defaults to 0 (the full match).

    • Optional "flags" - Javascript regex flags. Defaults to "gi"

  • Optional "replacement" - A replacement string value that will replace all instances of the search in the original string.

  • Optional "replacementDirect" - Directly input your own regex into the second argument of .search function for a replacement. This will override the replacement key.

If the replacement key is undefined (meaning the key does not exist), then your custom placeholder will return your search results.

If the replacement is defined, then your custom placeholder will return the original placeholder content with all instances of the search results replaced by your replacement string.

Chained Regex

More than one regexOp can be added for a placeholder by simply adding another regexOp object:

Feed
"regexOps": {
    "placeholder": [{
        "name": "whatever",
        "search": {
            "regex": "(?:\r\n|\r|\n)",
            "flags": "g"
        },
        "replacement": "\n(This will replace all new lines)"
    },{
        "name": "whatever",
        "search": {
            "regex": "^.*",
            "flags": "g"
        },
        "replacement": "This sentence will replace everything from the previous regexOp because of the ^.* search term (and because it has the same name)!"
    }]
}

The order and name matters! Named regexOps are executed from top to bottom.

Disabling

You can temporarily disable all regexOps, add the "disabled" key:

Feed
"regexOps": {
    "disabled": true,
    "title": [{
        "name": "whatever",
        "search": {
            "regex": "(?:\r\n|\r|\n)",
            "flags": "gi"
        },
        "replacement": ""
    }],
    "description": [{
        "name": "whatever",
        "search": {
            "regex": "(?:\r\n|\r|\n)",
            "flags": "g"
        },
        "replacement": ""
    },{
        "disabled": true,
        "name": "whatever",
        "search": {
            "regex": "(?:\r\n|\r|\n)",
            "flags": "g"
        },
        "replacement": ""
    }]
}

To disable multiple placeholders instead of disabling all regexOps, replace "disabled" boolean value with an array of whatever placeholders you want disabled: "disabled": ["description", "title"] or "disabled": ["title"]

You can also disable a specific regexOp by adding "disabled": true as well.

Errors

Regex errors will by default be shown for articles that the regex can't find a match for. To disable visible errors, change config.feeds.showRegexErrs to false.

Last updated