Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t删除“;C:\fakepath\";使用javascript替换和regex_Javascript_Jquery_Regex - Fatal编程技术网

Can';t删除“;C:\fakepath\";使用javascript替换和regex

Can';t删除“;C:\fakepath\";使用javascript替换和regex,javascript,jquery,regex,Javascript,Jquery,Regex,这个问题困扰了我一段时间。下面是问题代码的一部分: jQuery(':file').change(function() { var path = jQuery(this).val(); var filename = path.replace(/C:\\fakepath\\/, ''); jQuery(this).parent().find('.placetoshowfilename').html(filename); }); 我可以很好地获取文件路径,但是当我尝试使用regex

这个问题困扰了我一段时间。下面是问题代码的一部分:

jQuery(':file').change(function() {
    var path = jQuery(this).val();
    var filename = path.replace(/C:\\fakepath\\/, '');
jQuery(this).parent().find('.placetoshowfilename').html(filename);
});
我可以很好地获取文件路径,但是当我尝试使用regex从中删除“C:\fakepath\”部分时,我得到了“Uncaught SyntaxError:Invalid regular expression:missing/”错误。正则表达式应该是正确的,不是吗?你能告诉我,我做错了什么吗?提前谢谢

(这是WP,这就是为什么jQuery处于安全模式。)

编辑:嗯,它在JSFIDLE中起作用了,而且突然在我的页面上也起作用了。但我仍然不知道罪魁祸首是什么。

这在我的案件中起了作用

  $("#UploadPoFiles").change(function () {
        $("#uploader3").val(this.files && this.files.length ?
              this.files[0].name : this.value.replace(/^C:\\fakepath\\/i, ''));
 })
这在我的情况下奏效了

  $("#UploadPoFiles").change(function () {
        $("#uploader3").val(this.files && this.files.length ?
              this.files[0].name : this.value.replace(/^C:\\fakepath\\/i, ''));
 })

你所拥有的在我的浏览器控制台中工作。所以我只能假设您的js文件可能会被某个工具预处理。由于潜在问题与
/
符号有关,因此避免使用它可能会有所帮助
.replace()
方法也接受字符串。所以试试这个:

path.replace('C:\\fakepath\\', '');

你所拥有的在我的浏览器控制台中工作。所以我只能假设您的js文件可能会被某个工具预处理。由于潜在问题与
/
符号有关,因此避免使用它可能会有所帮助
.replace()
方法也接受字符串。所以试试这个:

path.replace('C:\\fakepath\\', '');
使用提取名称而不是删除路径:

var filename = path.match(/[^\\/]*$/)[0];
使用提取名称而不是删除路径:

var filename = path.match(/[^\\/]*$/)[0];

与其将整个路径作为字符串抓取,然后试图替换该字符串中的
C:\fakepath\
,您只需抓取不带路径前缀的文件名

const myFileInput = document.querySelector('input[type="file"]');
const myFileName = myFileInput.files[0].name;

与其将整个路径作为字符串抓取,然后试图替换该字符串中的
C:\fakepath\
,您只需抓取不带路径前缀的文件名

const myFileInput = document.querySelector('input[type="file"]');
const myFileName = myFileInput.files[0].name;

使用jQuery的replace和regex,jQuery没有
replace
或正则表达式。JavaScript有..replace(/^C:\\fakepath\\/i,,));如果你真的从输入中获得
C:\fakepath\file.txt
,你可以试试这个。使用一个文件输入。@T.J.Crowder即将发布相同的内容。即使这不是输入,发布的代码也不应该抛出该错误。旁注:你需要
.text(文件名)
,而不是
.html(文件名)
。但这不是问题所在(因为问题中没有任何错误)。“使用jQuery的replace和regex,jQuery没有
replace
或正则表达式。JavaScript没有..替换(/^C:\\fakepath\\/i,,);如果你真的从输入中得到
C:\fakepath\file.txt
,你能试试这个吗,使用文件输入。@T.J.Crowder即将发布相同的内容。即使这不是输入,发布的代码也不应该抛出该错误。旁注:您需要的是
.text(文件名)
,而不是
.html(文件名)
。但这不是问题所在(因为问题中没有任何错误)。谢谢!我相信你是对的,因为它在JSFIDLE中工作,并且它突然在我的页面上也开始工作。谢谢!我相信你是对的,因为它在JSFIDLE中工作,并且它突然在我的页面上也开始工作。