首页 > 解决方案 > 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

标签: javascriptphpregex

解决方案


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.


推荐阅读