我遇到了一个我无法理解的问题。这是一个小例子:
var Original="xxx?{AND tr.consumer = '$Msisdn$'}\nSomething\nSomething else",
replacement="AND tr.consumer = '$Msisdn$'",
replace="?{AND tr.consumer = '$Msisdn$'}"
然后,如果我执行Original.replace(replace,replacement)
我得到的是结果上的替换句子没有完成。这就是我得到的:
"AND tr.consumer = '$Msisdn"
我无法理解原因。 非常感谢。
答案 0 :(得分:2)
replace
函数适用于正则表达式以及普通字符串。因此,替换字符串也可以包含特殊字符,$
就是其中之一。你need to escape it:
var Original="xxx?{AND tr.consumer = '$Msisdn$'}\nSomething\nSomething else",
replacement="AND tr.consumer = '$$Msisdn$$'",
replace="?{AND tr.consumer = '$Msisdn$'}"
答案 1 :(得分:0)
好的,我检查了替换文档,有些人指出的是正确的。它是一个特殊的角色,应该被转义:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
我首先试图逃避它的思考方式"这是对的,但我做错了。我在做function __construct()
{
define('CLIENT_SECRET_PATH', 'client_secret_online.json');
$this->redirect_url=base_url()."plus_gmail/oauth2callback";
$this->client = new Google_Client();
$this->client->setIncludeGrantedScopes(true);
$this->client->setAuthConfigFile(CLIENT_SECRET_PATH);
$this->client->addScope(Google_Service_Plus::PLUS_LOGIN);
$this->client->addScope(Google_Service_Plus::PLUS_ME);
$this->client->addScope(Google_Service_Plus::USERINFO_EMAIL);
$this->client->addScope(Google_Service_Plus::USERINFO_PROFILE);
$this->client->addScope(Google_Service_Gmail::MAIL_GOOGLE_COM);
$this->client->setRedirectUri($this->redirect_url);
$this->client->setAccessType('offline');
$this->client->setIncludeGrantedScopes(true);
}
function index()
{
$auth_url = $this->client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
function oauth2callback()
{
if (! isset($_GET['code']))
{
echo "Error getting code"
}
else
{
$token=$_GET['code'];
$data["token"]=$token;
try
{
$this->client->authenticate($token);
$access_token = $this->client->getAccessToken();
$plus = new Google_Service_Plus($this->client);
$me=$plus->people->get('me');
$this->daouser->newUserPlus($me,$access_token);
var_dump($me);
$this->readEmailsId($this->client);
}
catch (Exception $e)
{
var_dump($e)
}
}
}
但正确的方法是:
\$
如果没有手动生成替换字符串,并且包含Pattern Inserts
$$ Inserts a "$".
个字符,您可以使用以下句子$