簡體   English   中英

根據多個分隔符 [/, #, @, ''] 拆分字符串

[英]Split a string based on multiple delimiters [/, #, @, '']

我想根據多個分隔符拆分字符串。

如何用多個字符串作為分隔符分割一個字符串?

例如:

我有一個字符串: "/create #channel 'name' 'description of the channel' #field1 #field2"

我想要一個數組:

/create
#channel
name
description of the channel
#field1
#field2

另一個例子,我有: "/send @user 'A messsage'"我想要:

/send
@user
A message

如何解決? 請問有什么幫助嗎? :-(

這里沒有 Regx 的解決方案

var multiSplit = function (str, delimeters) {
    var result = [str];
    if (typeof (delimeters) == 'string')
        delimeters = [delimeters];
    while (delimeters.length > 0) {
        for (var i = 0; i < result.length; i++) {
            var tempSplit = result[i].split(delimeters[0]);
            result = result.slice(0, i).concat(tempSplit).concat(result.slice(i + 1));
        }
        delimeters.shift();
    }
    return result;
}

簡單地使用

multiSplit("/create #channel 'name' 'description of the channel' #field1 #field2",['/','#','@',"'"])

輸出

Array [ "", "create ", "channel ", "name", " ", "description of the channel", " ", "field1 ", "field2" ]

您可以使用正則表達式

/([\/#]\w+|'[\s\w]+')/g

正則表達式說明: https : //regex101.com/r/lE4rJ7/3

  1. [\\/#@]\\w+ :這將匹配以#/@開頭的字符串
  2. | : 或條件
  3. '[\\s\\w]+' : 匹配用引號括起來的字符串

由於正則表達式也將匹配引號,因此需要將其刪除。

 var regex = /([\\/#@]\\w+|'[\\s\\w]+')/g; function splitString(str) { return str.match(regex).join().replace(/'/g, '').split(','); } var str1 = "/create #channel 'name' 'description of the channel' #field1 #field2 @Tushar"; var str2 = "/send @user 'A messsage'"; var res1 = splitString(str1); var res2 = splitString(str2); console.log(res1); console.log(res2); document.write(res1); document.write('<br /><br />' + res2);

這個適用於您的情況,但不適用於拆分:

('[^']+'|[^\\s']+)

注意:您仍然需要修剪單引號!

這是一個例子: http : //jsfiddle.net/k8s8r77e/1/

但我不能告訴你它是否總是有效。 以這種方式解析並不是正則表達式的真正用途。

像這樣的東西可能會起作用..但必須進行調整才能不消除撇號

var w = "/create #channel 'name' 'description of the channel' #field1 #field2";

var ar = w.split();

ar.forEach(function(e, i){  
    e = e.replace(/'/g, '')
    console.log(e)
})

小提琴: http : //jsfiddle.net/d9m9o6ao/

您提供的示例中有一些不一致的地方,您要么按這些字段拆分,要么將它們作為字段標記提供,但您似乎混合了。 以你的第一個例子為例,在原始字符串中你有 'name',但在結果中你把它去掉了 ' 並且只有名字,而 #channel 保留了 #。 無論如何,您有2個選擇:

String.split 采用正則表達式,因此您可以使用:

var re = /['\/@#"]+\s*['\/@#"]*/;

var str = "/create #channel 'name' 'description of the channel' #field1 #field2";
var parts = str.split(re);

你會得到一系列你的令牌。 請注意,在這種情況下,第一個是空的,只需在使用部分數組之前刪除空字符串。

或者,將 string.match 與正則表達式一起使用:

var re = /['\/@#"][^'\/@#"]*/g;
var parts = str.match(re);

ES6 版本避免使用正則表達式(常規語言可能很復雜,因為它最初是為 AI 設計的,在 unix 之前)

 const source = "this is a; string with; multiple delimiters; and no regex"; function multiSplit(str, delimiters) { return delimiters.reduce((acc, cur) => { if (typeof(acc) === 'string') { return acc.split(cur); } return acc.map(a => a.split(cur)).flat(1); }, str); } const split = multiSplit(source, [';', ' ']); console.log(split);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM