javascript - 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.
推荐阅读
- selenium - 使用 NUnit 框架测试测试用例时发生错误
- android - 带有 shouldInterceptRequest 覆盖的 Android WebView 即时崩溃
- ios - UIImage 不可变但 var
- html - Nginx 清理网址
- php - jQuery DataTable - 服务器端处理(分页和搜索不起作用)
- javascript - 为什么结果来了 2+'2' 结果是 22 而 2-'2' 结果是 0?
- npm - npm start 和 npm run start 的区别
- php - 通过 ESP8266 的 GET 请求不成功
- java - 带有 Apache POI 的折线图 - 错误
- version-control - 我可以获得所有 Powerbuilder 对象的历史记录吗?