0

I'm getting this error in java console: "SyntaxError: unterminated regular expression literal"

I really don't understand it. Below is my code, if someone could point out what am I missing I would be forever gratefull.

PHP Code:

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>';

Thanks.

2 Answers 2

2

The two slashes (\\) become one slash in the output of PHP (\). You have to write four slashes (\\\\).

Let's see the output of your current code:

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

The last backslash escapes the Regex terminator (the forward slash), therefore the regular expression terminal is unterminated.

Here is the output of the updated code:

this.value.replace(/^C:\\fakepath\\/i, "");
---------------------------------^^
escapes                           |
----------------------------------|

The last backslash won't affect anything because it is escaped by the backslash before it.

Sign up to request clarification or add additional context in comments.

Comments

1

You missed an escape slash here

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

Try like this

<?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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.