从字符第一次出现的地方分割字符串

发布于 2024-11-09 11:18:34 字数 271 浏览 1 评论 0原文

我在日志文件中有多行这种格式的文本:

topic, this is the message part, with, occasional commas.

如何从第一个逗号中分割字符串,以便将主题和消息的其余部分放在两个不同的变量中?

我尝试过使用这种拆分,但是当消息部分中有更多逗号时它不起作用。

[topic, message] = whole_message.split(",", 2);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南七夏 2024-11-16 11:18:34

使用正则表达式来获取“除了第一个逗号之外的所有内容”。因此:

whole_message.match(/([^,]*),(.*)/)

[1] 将是主题,[2] 将是消息。

Use a regex that gets "everything but the first comma". So:

whole_message.match(/([^,]*),(.*)/)

[1] will be the topic, [2] will be the message.

偏闹i 2024-11-16 11:18:34

这里!

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."]

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."]
π浅易 2024-11-16 11:18:34

这种分解赋值在 Javascript 中不起作用(目前)。试试这个:

var split = whole_message.split(',', 2);
var topic = split[0], message = split[1];

编辑 - 好的,所以“split()”有点坏了;试试这个:

var topic, message;
whole_message.replace(/^([^,]*)(?:,(.*))?$/, function(_, t, m) {
  topic = t; message = m;
});

That sort of decomposing assignment doesn't work in Javascript (at the present time). Try this:

var split = whole_message.split(',', 2);
var topic = split[0], message = split[1];

edit — ok so "split()" is kind-of broken; try this:

var topic, message;
whole_message.replace(/^([^,]*)(?:,(.*))?$/, function(_, t, m) {
  topic = t; message = m;
});
百善笑为先 2024-11-16 11:18:34

javascript 的 String.split() 方法已损坏(至少如果您期望其他语言的 split() 方法提供相同的行为)。

这种行为的一个例子:

console.log('a,b,c'.split(',', 2))
> ['a', 'b']

并不

> ['a', 'b,c']

像你想象的那样。

试试这个 split 函数:

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;
};  

javascript's String.split() method is broken (at least if you're expecting the same behavior that other language's split() methods provide).

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;
};  
贵在坚持 2024-11-16 11:18:34
var a = whole_message.split(",");
var topic = a.splice (0,1);

(除非你喜欢以复杂的方式做事)

var a = whole_message.split(",");
var topic = a.splice (0,1);

(unless you like doing things complicated ways)

丢了幸福的猪 2024-11-16 11:18:34

为什么不用逗号分割,将 [0] 项作为主题,然后从原始字符串中删除主题(+,)?

你可以:(

var topic = whole_message.split(",")[0]

使用prototype.js)

var message = whole_message.gsub(topic+", ", "") 

(使用jQuery)

whole_message.replace(topic+", ", "")

或者更快,使用josh.trow

Why not split by comma, take the [0] item as topic then remove the topic(+,) from the original string ?

You could:

var topic = whole_message.split(",")[0]

(using prototype.js)

var message = whole_message.gsub(topic+", ", "") 

(using jQuery)

whole_message.replace(topic+", ", "")

Or quicker, go with josh.trow

左岸枫 2024-11-16 11:18:34
let string="topic, this is the message part, with occasional commas."

let arr = new Array(); 
let idx = string.indexOf(',')
arr[0] = string.substring(0, idx);
arr[1] = string.substring(idx+1);
let topic = arr[0];
let message = arr[1]

输出 arr 应该是:[“主题”,“这是消息部分,偶尔有逗号。”]

let string="topic, this is the message part, with occasional commas."

let arr = new Array(); 
let idx = string.indexOf(',')
arr[0] = string.substring(0, idx);
arr[1] = string.substring(idx+1);
let topic = arr[0];
let message = arr[1]

output arr should be: ["topic", "this is the message part, with occasional commas."]

谁对谁错谁最难过 2024-11-16 11:18:34

使用 /,(.*)/ 或更确切地说 /, *(.*)/ 进行拆分以考虑逗号后面的空格

示例:

const str = "topic, this is, the, message."
const [topic, message] = str.split(/, *(.*)/);

console.log(topic);   // "topic"
console.log(message); // "this is, the, message."

Split using /,(.*)/ or rather /, *(.*)/ to account for the space after the comma

Example:

const str = "topic, this is, the, message."
const [topic, message] = str.split(/, *(.*)/);

console.log(topic);   // "topic"
console.log(message); // "this is, the, message."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文