简体   繁体   English

使用jquery或javascript拆分具有不同分隔符的字符串

[英]Split a string with varying separator using jquery or javascript

We can use javascript split method to split a string into an array of substrings. 我们可以使用javascript split方法将字符串拆分为子字符串数组。 Example: 例:

var timeformat="HH:MM:SS";
var timeformatarray=timeformat.split(":");

Is there a simple method to split a string if separator is not constant. 如果分隔符不是常量,是否有一种简单的方法来分割字符串。 Actually, I have to split timeformat that could come in any format like: 实际上,我必须分割时间格式,可以采用任何格式,如:

var timeformat="HH hr : MM min : SS sec";
var timeformat="HHhour:MMminute:SSsecond";
var timeformat="HHh MMm SSs";

Only constant would be HH, MM and SS. 只有常数是HH,MM和SS。 Timeformat is an option for the user to specify what is the format of the time that they want to display. Timeformat是用户指定他们想要显示的时间格式的选项。 "HH", "MM" and "SS" are constant text (Not numbers), these three are fixed constants that won't change. “HH”,“MM”和“SS”是常量文本(非数字),这三个是固定常量,不会改变。 Only thing that could change is the suffix and the separator in the timeformat string as shown in examples above. 只有可以改变的是时间格式字符串中的后缀和分隔符,如上例所示。

I want a method to split timeformat string into an array so that I can work on it. 我想要一个方法将timeformat字符串拆分成一个数组,以便我可以处理它。 I want the result be: 我希望结果是:

timeformat[0] = "HH"
timeformat[1] = " hr : " <- with spaces (if any)
timeformat[2] = "MM"
timeformat[3] = " min : "
timeformat[4] = "SS"
timeformat[5] = " sec"

With this array, I will format the time and add respective suffix and separators. 使用此数组,我将格式化时间并添加相应的后缀和分隔符。 I tried various methods, using regex and looping through each character, but they were not efficient and straight. 我尝试了各种方法,使用正则表达式并循环遍历每个字符,但它们并不高效和直接。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

Solution: I was able to resolve the issue by creating a method that works on the formatstring using regex, split and arrays. 解决方案:我能够通过使用正则表达式,拆分和数组创建一个处理formatstring的方法来解决问题。 I am sure there would be much better solution but I couldn't get any so here is my solution to the problem. 我相信会有更好的解决方案,但我无法得到任何解决问题的方法。 I would thank Stephen C for the direction on regex. 我要感谢Stephen C对正则表达式的指导。

function GetTimeFormatArray(timeformatstring){
        var timesuffixes = timeformatstring.split(/HH|MM|SS/);
        timesuffixes= $.grep(timesuffixes,function(n){
            return(n);
        });

        var pattern = timesuffixes.join('|');
        var timeprefixes = timeformatstring.split(new RegExp(pattern));
        timeprefixes = $.grep(timeprefixes,function(n){
            return(n);
        });

        var timeFormatArray = [];
        for(var i = 0; i < timesuffixes.length; i++){
            timeFormatArray.push(timeprefixes[i]);
            timeFormatArray.push(timesuffixes[i]);
        }
        return timeFormatArray;
    }

The function split() can take a regular expression as a delimiter. 函数split()可以将正则表达式作为分隔符。 Here's a sample but I'm no expert in regex so this might not be optimized. 这是一个示例,但我不是正则表达式的专家,所以这可能不会被优化。

var test = 'HH hr : MM min : SS sec';

//split by ":" or space with optional leading and trailing space
console.log(test.split(/\s?[\s:]\s?/));​
//["HH", "hr", "MM", "min", "SS", "sec"]

If I was doing this in Java, I'd compose the delimiter regex from look-aheads and look-behinds . 如果我在Java中这样做,我会从前瞻和后视组成分隔符正则表达式。 Unfortunately, Javascript regexes don't support look-behind. 不幸的是,Javascript正则表达式不支持后视。

I think you need to do this the hard way. 我认为你需要以艰难的方式做到这一点。 Match the string with something like 匹配字符串与类似的东西

    /(.*)HH(.*)MM(.*)SS(.*)/

and then index the array returned by the matcher. 然后索引匹配器返回的数组。 If the HH / MM / SS can appear in any order, etcetera you may need a more complicated regex like this: 如果HH / MM / SS可以按任何顺序出现,等等你可能需要一个更复杂的正则表达式,如下所示:

    /(.*?)(?:(HH|MM|SS)(.*?))*/

This is using non-eager matching and a non-capturing group. 这是使用非渴望匹配和非捕获组。 You'd have to deal with cases like "HHMMSS" (ie no space between the "separators") and "SS one SS" (multiple instances of the same "delimiter"). 您必须处理诸如“HHMMSS”(即“分隔符”之间没有空格)和“SS one SS”(同一“分隔符”的多个实例)之类的情况。 Note also that this kind of pattern is rather dangerous, since a carefully crafted input can trigger huge amounts of back-tracking. 另请注意,这种模式非常危险,因为精心设计的输入可以触发大量的反向跟踪。

var rx = /^(\d{2})(.*?)(\d{2})(.*?)(\d{2})(.*?)$/
var timeformat = [
    "11 hr : 22 min : 33 sec",
    "11hour:22minute:33second",
    "11h 22m 33s"
];

for (var i = 0; i < timeformat.length; i++) {
    console.log(timeformat[i])
    try {
        for (var j = 0; j < timeformat[i].match(rx).length; j++) {
            console.log('\tmatch['+j+'] = ',timeformat[i].match(rx)[j]);
        }
    } catch(e) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM