问题标签 [variable-substitution]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
152 浏览

regex - 如何避免 Perl 正则表达式中的变量替换?

我有以下正则表达式:

它应该做的是:检查 xmldirname 是否以“db”开头 ($) 并将字符串的其余部分放入 $pkg。

它的作用是:“全局符号 $db 需要明确的包名称”。

我该怎么做呢?使用 qr// 也不起作用。

是的,好的,我明白了。$ 是结束,而不是开始。对不起....

0 投票
1 回答
206 浏览

bash - 在替代值扩展中使用字符串作为 bash 变量名

如何在 bash中的替代值扩展( ${var+alt})中使用一个变量的值作为另一个变量的名称?

我会认为

应该大致相当于

但是当我尝试这样做时,我得到

我想部分问题是执行命令替换的子shell不再知道$varname了?我需要导出该变量吗?

我这样做的原因是我从这个答案中学到了如何检查变量是否为空,并且我试图将该检查封装在一个名为 的函数中is_null,如下所示:

0 投票
2 回答
242 浏览

bash - 将波浪号扩展与bash中的变量替换相结合

假设我以 root 身份运行 bash,并且我想 chown 用户的主目录ada。还要说它ada存储在一个名为的变量中$USER(因为我是从脚本中执行此操作的)。

当我尝试以下操作时,

外壳首先执行波浪号扩展,因此它尝试 chown /home/root/ada,而不是/home/ada.

有没有办法通过嵌套的替换和正确的转义来做到这一点?

0 投票
2 回答
852 浏览

r - R 是否支持间接变量扩展或变量替换?

在 bash 中,可以使用感叹号将变量的值用作变量。请参阅此处的说明。这称为变量间接扩展。这可用于在代码中使用另一个变量来命名新变量。我想知道这是否可以在 R 语言中完成。

例如,假设我想为位于公司每个建筑物的员工创建一个数据框。

并让“ ♠building ”成为我想要间接扩展的变量。所以我想使用建筑物名称作为该建筑物名称的新变量。我在这里使用 ♠ 字符,因为使用 ! (因为它在 bash 中的变量前面使用)引起了一些混乱。

如果这在 R 中是可能的,那么预期的输出将是创建三个新的数据框,这些数据框为 for 循环找到的每个建筑物命名。圆周率、E 和 C:

有没有办法使用建筑物名称作为新值的名称?所以代替♠building变量的值将被替换为变量名。我可以在 bash 中做到这一点,并且想知道这在 R 中是否可行。

0 投票
1 回答
50 浏览

python - 在 Python 中将 Linux 样式替换转换为 Windows 样式替换

有没有办法将 Linux 样式的 shell 替换转换为 Python 中的 Windows 替换?

假设在 Linux 环境中有一个命令:

在 Windows 中:

我想要它,如果我的环境是 Windows,那么我可以接受一个命令并将其替换为 Windows 样式替换:

样本输入/输出:

谢谢!

0 投票
2 回答
1400 浏览

python - Python:在传递给另一个函数之前替换变量值

我想确定输入字符串是否是有效的函数名。在传递给 isfunction 调用之前有什么方法可以替换变量的值?

0 投票
2 回答
392 浏览

sql - Tcl SQLite update variable substitution cannot have apostrophe

Here's the problem: if I use { } for the update command like so:

I cannot substitute any variables inside the curly brackets. it all has to be hard coded.

However, if I use " " for the update command like so:

I can substitute variables inside the double quotes, but I must use ' ' in order to put in data with spaces so sql sees it as one input. If I don't I get an error if I send something like

$data = "Legit Stack"

Because it has a space the sql will choke on the word: Stack unless it is placed within single quotes

Therefore...

If I send this data to the update command:

I get the following error:

near "s": syntax error while executing "fileRepo eval "UPDATE uploads SET $col='$data' WHERE rowid=$id" ...

Thus given these rules I can see no way to pass a single quote or apostrophe to the update command successfully. Is there a different way to do this?

Thanks!

0 投票
1 回答
5069 浏览

groovy - Gradle 中的变量替换何时需要花括号?

如果您在gradle.properties文件中定义了变量,则何时需要使用花括号进行变量替换(例如,"some string ${yourVariable}")以及何时可以不使用(例如,"some string $yourVariable")。始终使用花括号是否被认为是最佳做法?

0 投票
1 回答
36 浏览

bash - Bash 变量 - 定义多个可能的期​​望值

抱歉,如果以前已经回答过这个问题,但我在网上找不到太多。

这是我要完成的工作:

这样,以下任何一项都将满足条件:fooservername01、barservername02、fooservername03、barservername03 等。

我以一种效率低得多的方式完成了这项工作:

但我希望优化/简化代码。

任何见解都将不胜感激,因为我对 bash 脚本甚至一般脚本仍然很陌生。

TIA

0 投票
2 回答
43 浏览

bash - 变量是否有等效于 <(process list) 的变量?

我可以使用<(some command)代替文件,但是如何使用变量实现等效?我正在使用该paste命令,该命令需要文件作为参数,并且想要使用它的内容,SOMEVAR就好像它是一个文件一样。

更新0

我希望<<<(SOMEVAR)存在类似的东西。<(echo $SOMEVAR)不起作用,因为 echo 在参数之间插入空格,所以换行符丢失。