php打印中的JavaScript语法错误

php打印中的JavaScript语法错误,javascript,php,Javascript,Php,我在java控制台中遇到这个错误:“SyntaxError:unterminated regular expression literal” 我真的不明白。下面是我的代码,如果有人能指出我错过了什么,我将永远感激 PHP代码: print ' <script type="text/javascript"> function fakeUpload() { $("#fakeupload").val(this.files && this.files.length ?

我在java控制台中遇到这个错误:“SyntaxError:unterminated regular expression literal”

我真的不明白。下面是我的代码,如果有人能指出我错过了什么,我将永远感激

PHP代码:

print '
<script type="text/javascript">
function fakeUpload() {
    $("#fakeupload").val(this.files && this.files.length ? this.files[0].name : this.value.replace(/^C:\\fakepath\\/i, ""));
}
</script>';
打印'
函数fakeUpload(){
$(“#fakeupload”).val(this.files&&this.files.length?this.files[0]。名称:this.value.replace(/^C:\\fakepath\\\/i,”);
}
';

谢谢。

你错过了一条逃生路线

this.value.replace(/^C:\\fakepath\\//i, ""));
                                    ^
像这样试试

<?php
print '
<script type="text/javascript">
function fakeUpload() {
    $("#fakeupload").val(this.files && this.files.length ? this.files[0].name : this.value.replace(/^C:\\fakepath\\//i, ""));
}
</script>';

您错过了此处的转义斜杠

this.value.replace(/^C:\\fakepath\\//i, ""));
                                    ^
像这样试试

<?php
print '
<script type="text/javascript">
function fakeUpload() {
    $("#fakeupload").val(this.files && this.files.length ? this.files[0].name : this.value.replace(/^C:\\fakepath\\//i, ""));
}
</script>';
两个斜杠(
\\
)在PHP的输出中变成一个斜杠(
\
)。你必须写四个斜杠(
\\\\

让我们看看当前代码的输出:

this.value.replace(/^C:\fakepath\/i, "");
this.value.replace(/^C:\\fakepath\\/i, "");
---------------------------------^^
escapes                           |
----------------------------------|
最后一个反斜杠转义正则表达式终止符(正斜杠),因此正则表达式终止符是未终止的

以下是更新代码的输出:

this.value.replace(/^C:\fakepath\/i, "");
this.value.replace(/^C:\\fakepath\\/i, "");
---------------------------------^^
escapes                           |
----------------------------------|
最后一个反斜杠不会影响任何东西,因为它被它前面的反斜杠转义。

两个斜杠(
\\
)在PHP的输出中变成一个斜杠(
\
)。你必须写四个斜杠(
\\\\

让我们看看当前代码的输出:

this.value.replace(/^C:\fakepath\/i, "");
this.value.replace(/^C:\\fakepath\\/i, "");
---------------------------------^^
escapes                           |
----------------------------------|
最后一个反斜杠转义正则表达式终止符(正斜杠),因此正则表达式终止符是未终止的

以下是更新代码的输出:

this.value.replace(/^C:\fakepath\/i, "");
this.value.replace(/^C:\\fakepath\\/i, "");
---------------------------------^^
escapes                           |
----------------------------------|
最后一个反斜杠不会影响任何东西,因为它被它前面的反斜杠转义