简体   繁体   中英

RegExp (javascript) :: find a group of php variables including the $ character (Enlighter JS)

I'm trying to find several custom php variables out of a whole "code block" string to enlighten them via EnlighterJS (v3.3) https://github.com/EnlighterJS/EnlighterJS/commit/a5854c3455b68790aa21d56e2ceb7b734dd72913

In my php code block I'm using a bunch of repeating custom variables which could be considered most likely as global constants like:

$TIME_MS, $GET_URL, $FILE_TYPE

I managed to enlighten the constants without the $ character:

regex: /\b(TIME_MS|GET_URL|FILE_TYPE)\b

But how can I catch the $ character as well? This didn't work:

/\b(\$TIME_MS|\$GET_URL|\$FILE_TYPE)\b

Thanks and best,

Maxxx

Word boundaries won't work in front of a $ character, because this is not a word character. \b finds boundaries between word and non-word characters. Instead, you may use (?<!\S)\$ :

(?<!\S)\$(TIME_MS|GET_URL|FILE_TYPE)\b

Demo

If your regex tool does not support lookbehinds, then perhaps you can just search for a space character before the $ . Though, this would not catch cases where a $ variable happens to begin the line or input.

This one seems to work now:

    { 
      regex: /(\$TIME_MS|\$GET_URL|\$FILE_TYPE)\b/gi,
      type: "gvar",
    },

DEMO

Correct me if I missed something like line breaks and stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM