JavaScript字符串修改(可能涉及正则表达式)

JavaScript string modification (Probably involves regular expression)

本文关键字:正则表达式 字符串 修改 JavaScript      更新时间:2023-09-26

我需要解决的问题是缩短用户提供的文件路径。如果您不知道,有时无法在命令提示符下输入带有空格的路径。您需要将路径用引号括起来,或者用空格将路径重命名为"abcdef~1"。

示例:"C:''Some Folder''Some-File.exe"应变为"C:''SomeFo~1''SomeFi~1.exe"(不区分大小写)。

我正在JavaScript中制作一个函数,试图使用这个想法缩短文件路径。

function ShortenFilePath(FilePath){
    var Sections = FilePath.split("''")
    for (Index = 0; Index < Sections.length; Index++){
        while (Sections[Index].length > 6 && Sections[Index].match(" ") && !Sections[Index].match("~1")){
            alert(Sections[Index])
            Sections[Index] = Sections[Index].replace(" ","")
            Sections[Index] = Sections[Index].substring(0,6)
            Sections[Index] = Sections[Index] + "~1"
            alert(Sections[Index])
        }
    }
    var FilePath = Sections.join("''")
    alert(FilePath)
    return FilePath
}

问题是,它会省略文件扩展名,并吐出"C:''SomeFo~1''SomeFi~1"。我需要帮助获得那个文件扩展名(可能是通过正则表达式)。如果你觉得这个功能可以优化,请分享你的想法。

更新:我相信问题已经解决了。

更新2:以前的代码有一些问题,所以我对它进行了一些修改。

更新3:新问题。诶呀如果没有扩展名的文件本身的名称在7个字母以下,那么它将显示为"name.e~1.exe".

更新4:我想我终于解决了这个问题。我想。

function ShortenFilePath(FilePath){
    var Sections = FilePath.split("''")
    Sections[Sections.length - 1] = Sections[Sections.length - 1].substring(0,Sections[Sections.length - 1].lastIndexOf("."))
    for (Index = 0; Index < Sections.length; Index++){
        while (Index > 0 && Sections[Index].match(" ") && !Sections[Index].match("~1")){
            Sections[Index] = Sections[Index].replace(/ /gm,"")
            Sections[Index] = Sections[Index].substring(0,6) + "~1"
        }
    }
    return Sections.join("''") + FilePath.substring(FilePath.lastIndexOf("."))
}

我会用它来获得扩展:

someString.substring(someString.lastIndexOf("."))

你还要求进行一些代码审查,所以:

1-你的JS约定有点偏离,它看起来更像C#:)为什么变量和方法名称中有大写字母?

2-你说你可以使用引号选项,而不是使用~1,看起来更容易,为什么你决定不这样做?

3-为什么你的JS中需要这样的东西?

这个怎么样:

function ShortenFilePath(FilePath){
    var Sections = FilePath.split("''")
    var suffix = FilePath.match(/('..*$)/)
    for (Index = 0; Index < Sections.length; Index++){
        while (Sections[Index].length > 6 && Sections[Index].match(" ") && !Sections[Index].match("~1")){
            alert(Sections[Index])
            Sections[Index] = Sections[Index].replace(" ","")
            Sections[Index] = Sections[Index].substring(0,6)
            Sections[Index] = Sections[Index] + "~1"
            alert(Sections[Index])
        }
    }
    var FilePath = Sections.join("''") + (suffix? suffix[1] : '')
    alert(FilePath)
    return FilePath
}

您可以使用带有回调的String.prototype.replace

function ShortenFilePath(FilePath){
    return FilePath.replace(/([^:'']+)([''|'.[^'']+)/g, function(text,match1, match2){
        return match1.length > 8 ? match1.replace(' ', '').substr(0, 6) + '~1' + match2 : match1.replace(' ', '') + match2;
    });    
}

我不能100%确定这会确切地输出你需要的东西,但你可能已经明白了:)
jsFiddle

修复更新#3问题:

if (FilePath.lastIndexOf(".") > 6){
   Sections[Index] = Sections[Index].substring(0,6) + "~1"
} else {
   Sections[Index] = Sections[Index].substring(0, FilePath.lastIndexOf(".")) + "~1"
}

顺便说一句,这个:

while (Sections[Index].match(" ")){
   Sections[Index] = Sections[Index].replace(" ","")
}

应该看起来像这样:

Sections[Index] = Sections[Index].replace(/ /gm, "");