【发布时间】:2022-01-17 01:42:46
【问题描述】:
我想用下划线替换所有在特定字符串中的点。就我而言,我在我的应用程序中使用 i18n(用于翻译),并且使用“$t(key)”调用键。 键可以是这样的:
key = 'translationKey'
key = 'translationKey.subkey.subSubkey'
key = 'translationKey.subkey.one'
key = 'translationKey.subkey.zero'
key = 'translationKey.subkey.others'
在 $t 上下文中:我想将后面没有 one 或 zero 或 others 的点 (.) 转换为下划线 (_)。如果我有这些输入:
foo.toto.anything
$t('translationKey')
$t('translationKey.subkey.subSubkey')
$t('translationKey.subkey.one')
$t('translationKey.subkey.zero')
$t('translationKey.subkey.others')
那么输出将是:
foo.toto.anything
$t('translationKey')
$t('translationKey_subkey_subSubkey')
$t('translationKey_subkey.one')
$t('translationKey_subkey.zero')
$t('translationKey_subkey.others')
我想使用 Vscode 正则表达式搜索来捕获这些点并替换它们(如果可能的话)。
我只设法获得了目标点所在的所有字符串,但我不能只到达这些点。这是我的正则表达式:(\$|\.)t?t(c|p)?('(\w+\.?)*')
谢谢
【问题讨论】:
-
你试过了吗?
-
@depperm 实际上我被卡住了,因为我只设法获得了目标点所在的所有字符串,但我不能只到达这些点。这是我的正则表达式: (\$|\.)t?t(c|p)?('(\w+\.?)*')
-
@K450 谢谢你,但是这样你就可以捕捉到所有其他的点。不仅是 $t(translationKey.subKey.etc); 中的那些;
-
使用
(?<=\$t\('[^']*)\.(?!(?:one|zero|others)')(?=[^']*'\))。请将您使用的正则表达式添加到问题正文中。 -
也许你会喜欢
.replace(/\b\.(?!one|zero|other|$)/g,"_")。
标签: javascript regex visual-studio-code