用于捕获和用引号替换字符串的正则表达式

RegEx for capturing and replacing a string with quotes

我想 str_replace 在函数中。

尝试

str_replace(array('(', ')', array('"', '\'')), array('\(', '\)', '["|\']'), 'hello("test")');

期望的输出:

hello\(["|']test["|']\)

这会起作用,但不是很有用:

str_replace(array('"', '\''), '["|\']', str_replace(array('(', ')'), array('\(', '\)'), 'hello("test")'));

如何解决这个问题?

在这里,我们可能想要捕获 hellotest,然后 assemble 我们希望使用 preg_replace:

$re = '/(.*)\("(.+)"\)/m';
$str = 'hello("test")';
$subst = "\([\"|'][\"|'\"]\)";

$result = preg_replace($re, $subst, $str);

echo $result;

输出

hello\(["|']test["|'"]\)

正则表达式

正则表达式

您可以 modify/change 在 regex101.com 中表达您的表情。

正则表达式电路

您还可以在 jex.im:

中可视化您的表情