jquery 字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5873743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:56:34  来源:igfitidea点击:

jquery string replace

jquery

提问by Pluda

need help on this, trying to upload images from IE9 I get an C:fakepath/name_of_my_file

在这方面需要帮助,尝试从 IE9 上传图像我得到一个 C:fakepath/name_of_my_file

how do I remove this C:fakepath?

我如何删除这个 C:fakepath?

Thanks

谢谢

if($('#ficheiro').val().search(/C:fakepath/)) {
                $('#ficheiro').val().val($('#ficheiro').val().replace('C:fakepath',''));
                nome.val('pics/'+$('#ficheiro').val());
            } else {
                nome.val('pics/'+$('#ficheiro').val());
            }

回答by André Figueira

You can use this and it will also remove the slashes leaving you just the filename.

您可以使用它,它还会删除斜线,只留下文件名。

$(this).val().replace(/C:\fakepath\/i, '');

回答by Chandu

Can't you simply use the replace function of string?

不能简单的使用string的replace功能吗?

nome.val("pics/" + $('#ficheiro').val().replace("C:fakepath", ""));

回答by powpow

var path = new String($('#ficheiro').val());
path = path.replace("C:fakepath", "");

var path = new String($('#ficheiro').val());
path = path.replace("C:fakepath", "");

回答by Ernesto

Have a solution for you, firts check for browser beeen IE, next use encodeURI to encode all the file path and name, you have to do that first in order to capture correctly the unscaped chars like "\". Then just replace, its working for me:

给你一个解决方案,首先检查浏览器beeen IE,接下来使用encodeURI对所有文件路径和名称进行编码,你必须先这样做才能正确捕获像“\”这样的未转义字符。然后只需更换,它对我有用:

var browserName=navigator.appName; 
if (browserName=="Microsoft Internet Explorer")
{
    var soloNombre = encodeURI(soloNombre);
    soloNombre = soloNombre.replace("C:%5Cfakepath%5C","");
    var soloNombre = decodeURI(soloNombre);
    alert(soloNombre);
}

Works like a charm.

奇迹般有效。

回答by user3394294

Try this

尝试这个

$(function() {
     $("input:file").change(function (){
       var fileName = $(this).val().replace("C:\fakepath\", "");
     });
  });