我如何分裂一个字符串与多个分隔符在JavaScript?
我试图拆分逗号和空格,但AFAIK JavaScript的split()函数只支持一个分隔符。
我如何分裂一个字符串与多个分隔符在JavaScript?
我试图拆分逗号和空格,但AFAIK JavaScript的split()函数只支持一个分隔符。
当前回答
在Github上查看我的简单库
如果你真的不想访问或与repo交互,下面是工作代码:
/**
*
* @param {type} input The string input to be split
* @param {type} includeTokensInOutput If true, the tokens are retained in the splitted output.
* @param {type} tokens The tokens to be employed in splitting the original string.
* @returns {Scanner}
*/
function Scanner(input, includeTokensInOutput, tokens) {
this.input = input;
this.includeTokensInOutput = includeTokensInOutput;
this.tokens = tokens;
}
Scanner.prototype.scan = function () {
var inp = this.input;
var parse = [];
this.tokens.sort(function (a, b) {
return b.length - a.length; //ASC, For Descending order use: b - a
});
for (var i = 0; i < inp.length; i++) {
for (var j = 0; j < this.tokens.length; j++) {
var token = this.tokens[j];
var len = token.length;
if (len > 0 && i + len <= inp.length) {
var portion = inp.substring(i, i + len);
if (portion === token) {
if (i !== 0) {//avoid empty spaces
parse[parse.length] = inp.substring(0, i);
}
if (this.includeTokensInOutput) {
parse[parse.length] = token;
}
inp = inp.substring(i + len);
i = -1;
break;
}
}
}
}
if (inp.length > 0) {
parse[parse.length] = inp;
}
return parse;
};
用法很简单:
var tokens = new Scanner("ABC+DE-GHIJK+LMNOP", false , new Array('+','-')).scan();
console.log(tokens);
给:
['ABC', 'DE', 'GHIJK', 'LMNOP']
如果您希望在输出中包含分隔令牌(+和-),将false设置为true,瞧!它仍然有效。
现在的用法是:
var tokens = new Scanner("ABC+DE-GHIJK+LMNOP", true , new Array('+','-')).scan();
and
console.log(tokens);
将:
['ABC', '+', 'DE', '-', 'GHIJK', '+', 'LMNOP']
享受吧!
其他回答
我使用regexp:
str = 'Write a program that extracts from a given text all palindromes, e.g. "ABBA", "lamal", "exe".';
var strNew = str.match(/\w+/g);
// Output: ["Write", "a", "program", "that", "extracts", "from", "a", "given", "text", "all", "palindromes", "e", "g", "ABBA", "lamal", "exe"]
让我们保持简单:(在你的RegEx中添加“[]+”表示“1或更多”)
这意味着“+”和“{1,}”是相同的。
var words = text.split(/[ .:;?!~,`"&|()<>{}\[\]\r\n/\\]+/); // note ' and - are kept
我发现我需要这样做的主要原因之一是在/和\上拆分文件路径。这是一个有点棘手的正则表达式,所以我将它贴在这里作为参考:
var splitFilePath = filePath.split(/[\/\\]/);
下面是在ES6中实现相同的新方法:
SplitByString(source, splitBy) { var splitter = splitBy.split("); splitter.push([源]);//推送初始值 返回分配器。reduceRight(函数(累加器,curValue) { Var k = []; 蓄电池。v => k =[…k…v.split (curValue)]); 返回k; }); } Var source = "abc,def#hijk*lmn,opq#rst*uvw,xyz"; var splitBy = ",*#"; splitBy console.log (SplitByString(源));
在这个函数中请注意:
不涉及正则表达式 以源代码中显示的相同顺序返回拆分后的值
以上代码的结果为:
My refactor of @Brian answer var string = 'and this is some kind of information and another text and simple and some egample or red or text'; var separators = ['and', 'or']; function splitMulti(str, separators){ var tempChar = 't3mp'; //prevent short text separator in split down //split by regex e.g. \b(or|and)\b var re = new RegExp('\\b(' + separators.join('|') + ')\\b' , "g"); str = str.replace(re, tempChar).split(tempChar); // trim & remove empty return str.map(el => el.trim()).filter(el => el.length > 0); } console.log(splitMulti(string, separators))