javascript 从第一次出现的字符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6131195/
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
Splitting string from the first occurrence of a character
提问by Seerumi
I have multiple lines of text in log files in this kind of format:
我在这种格式的日志文件中有多行文本:
topic, this is the message part, with, occasional commas.
How can I split the string from the first comma so I would have the topic and the rest of the message in two different variables?
如何从第一个逗号拆分字符串,以便我将主题和消息的其余部分放在两个不同的变量中?
I've tried using this kind of split, but it doesn't work when there's more commas in the message part.
我试过使用这种拆分,但是当消息部分中有更多逗号时它不起作用。
[topic, message] = whole_message.split(",", 2);
回答by Ian
Use a regex that gets "everything but the first comma". So:
使用获取“除第一个逗号之外的所有内容”的正则表达式。所以:
whole_message.match(/([^,]*),(.*)/)
[1]
will be the topic, [2]
will be the message.
[1]
将是主题,[2]
将是消息。
回答by Pointy
That sort of decomposing assignment doesn't work in Javascript (at the present time). Try this:
这种分解分配在 Javascript 中不起作用(目前)。试试这个:
var split = whole_message.split(',', 2);
var topic = split[0], message = split[1];
edit— ok so "split()" is kind-of broken; try this:
编辑— 好的,所以“split()”有点坏了;试试这个:
var topic, message;
whole_message.replace(/^([^,]*)(?:,(.*))?$/, function(_, t, m) {
topic = t; message = m;
});
回答by josh.trow
Here!
这里!
String.prototype.mySplit = function(char) {
var arr = new Array();
arr[0] = this.substring(0, this.indexOf(char));
arr[1] = this.substring(this.indexOf(char) + 1);
return arr;
}
str = 'topic, this is the message part, with, occasional commas.'
str.mySplit(',');
-> ["topic", " this is the message part, with, occasional commas."]
回答by digitalbath
javascript's String.split()
method is broken (at least if you're expecting the same behavior that other language's split()
methods provide).
javascript 的String.split()
方法被破坏了(至少如果您期望其他语言的split()
方法提供相同的行为)。
An example of this behavior:
这种行为的一个例子:
console.log('a,b,c'.split(',', 2))
> ['a', 'b']
and not
并不是
> ['a', 'b,c']
like you'd expect.
就像你期望的那样。
Try this split function instead:
试试这个拆分函数:
function extended_split(str, separator, max) {
var out = [],
index = 0,
next;
while (!max || out.length < max - 1 ) {
next = str.indexOf(separator, index);
if (next === -1) {
break;
}
out.push(str.substring(index, next));
index = next + separator.length;
}
out.push(str.substring(index));
return out;
};
回答by rob
var a = whole_message.split(",");
var topic = a.splice (0,1);
(unless you like doing things complicated ways)
(除非你喜欢用复杂的方式做事)
回答by Reuben Mallaby
Why not split by comma, take the [0] item as topic then remove the topic(+,) from the original string ?
为什么不用逗号分隔,将 [0] 项作为主题,然后从原始字符串中删除主题(+,)?
You could:
你可以:
var topic = whole_message.split(",")[0]
(using prototype.js)
(使用prototype.js)
var message = whole_message.gsub(topic+", ", "")
(using jQuery)
(使用jQuery)
whole_message.replace(topic+", ", "")
Or quicker, go with josh.trow
或者更快,使用 josh.trow