第一个空格出现分割字符串

时间:2022-08-22 13:16:14

I didn't get an optimized regex that split me a String basing into the first white space occurrence:

我没有得到一个优化的regex,它将一个基于字符串分割为第一个空格:

var str="72 tocirah sneab";

I need to get:

我需要:

72
tocirah sneab

10 个解决方案

#1


236  

If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

如果您只关心空格字符(而不关心制表符或其他空格字符),并且只关心第一个空格前的所有内容和第一个空格后的所有内容,那么您可以不使用如下的正则表达式:

str.substr(0,str.indexOf(' ')); // "72"
str.substr(str.indexOf(' ')+1); // "tocirah sneab"

Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).

注意,如果根本没有空格,那么第一行将返回一个空字符串,第二行将返回整个字符串。确保这是你在这种情况下想要的行为(或者这种情况不会出现)。

#2


34  

Javascript doesn't support lookbehinds, so split is not possible. match works:

Javascript不支持后来居上,所以拆分是不可能的。匹配工作原理:

str.match(/^(\S+)\s(.*)/).slice(1)

Another trick:

另一个技巧:

str.replace(/\s+/, '\x01').split('\x01')

how about:

如何:

[str.replace(/\s.*/, ''), str.replace(/\S+\s/, '')]

and why not

为什么不

reverse = function (s) { return s.split('').reverse().join('') }
reverse(str).split(/\s(?=\S+$)/).reverse().map(reverse)

or maybe

或者

re = /^\S+\s|.*/g;
[].concat.call(re.exec(str), re.exec(str))

#3


14  

In ES6 you can also

在ES6中你也可以

let [first, ...second] = str.split(" ")
second = second.join(" ")

#4


11  

var arr = [];             //new storage
str = str.split(' ');     //split by spaces
arr.push(str.shift());    //add the number
arr.push(str.join(' '));  //and the rest of the string

//arr is now:
["72","tocirah sneab"];

but i still think there is a faster way though.

但是我仍然认为有一个更快的方法。

#5


7  

You can also use .replace to only replace the first occurrence,

你也可以使用。replace只替换第一个出现,

​str = str.replace(' ','<br />');

Leaving out the /g.

离开/ g。

DEMO

演示

#6


5  

georg's solution is nice, but breaks if the string doesn't contain any whitespace. If your strings have a chance of not containing whitespace, it's safer to use .split and capturing groups like so:

georg的解决方案很好,但是如果字符串不包含任何空格,就会中断。如果您的字符串有可能不包含空格,那么使用.split和捕获组(如:

str_1 = str.split(/\s(.+)/)[0];  //everything before the first space
str_2 = str.split(/\s(.+)/)[1];  //everything after the first space

#7


4  

Late to the game, I know but there seems to be a very simple way to do this:

我知道这是一场迟来的比赛,但似乎有一种非常简单的方法:

var str = "72 tocirah sneab";
var arr = str.split(/ (.*)/);

This will leave arr[0] with "72" and arr[1] with "tocirah sneab".

这将留下arr[0]和“72”,arr[1]和“tocirah sneab”。

For reference:

供参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split Capturing_parentheses

#8


2  

Just split the string into an array and glue the parts you need together. This approach is very flexible, it works in many situations and it is easy to reason about. Plus you only need one function call.

只需将字符串分割成一个数组,然后将需要的部分粘在一起。这种方法非常灵活,在许多情况下都适用,而且很容易推理。加上你只需要一个函数调用。

arr = str.split(' ');             // ["72", "tocirah", "sneab"]
strA = arr[0];                    // "72"
strB = arr[1] + ' ' + arr[2];     // "tocirah sneab"

Alternatively, if you want to cherry-pick what you need directly from the string you could do something like this:

或者,如果你想直接从字符串中选择你需要的东西,你可以这样做:

strA = str.split(' ')[0];                    // "72";
strB = str.slice(strA.length + 1);           // "tocirah sneab"

Or like this:

或者像这样:

strA = str.split(' ')[0];                    // "72";
strB = str.split(' ').splice(1).join(' ');   // "tocirah sneab"

However I suggest the first example.

但是我建议第一个例子。

Working demo: jsbin

工作演示:jsbin

#9


1  

Whenever I need to get a class from a list of classes or a part of a class name or id, I always use split() then either get it specifically with the array index or, most often in my case, pop() to get the last element or shift() to get the first.

每当我需要得到一个类从一个列表的类或类名称或id的一部分,我总是使用分割()然后把它与数组索引或特别,常常在我的例子中,pop()来获得最后一个元素或转移()来获得第一。

This example gets the div's classes "gallery_148 ui-sortable" and returns the gallery id 148.

这个示例获取div的类“gallery_148 ui-sortable”并返回gallery id 148。

var galleryClass = $(this).parent().prop("class"); // = gallery_148 ui-sortable
var galleryID = galleryClass.split(" ").shift(); // = gallery_148
galleryID = galleryID.split("_").pop(); // = 148
//or
galleryID = galleryID.substring(8); // = 148 also, but less versatile 

I'm sure it could be compacted into less lines but I left it expanded for readability.

我确信它可以被压缩成更少的行,但我保留了它的可读性。

#10


0  

I needed a slightly different result.

我需要一个稍微不同的结果。

I wanted the first word, and what ever came after it - even if it was blank.

我想要的是第一个词,以及它之后的一切——即使它是空白的。

str.substr(0, text.indexOf(' ') == -1 ? text.length : text.indexOf(' '));
str.substr(text.indexOf(' ') == -1 ? text.length : text.indexOf(' ') + 1);

so if the input is oneword you get oneword and ''.

所以如果输入是一个单词,你得到一个单词。

If the input is one word and some more you get one and word and some more.

如果输入是一个单词和更多的单词,你会得到一个单词和更多的单词。

#1


236  

If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

如果您只关心空格字符(而不关心制表符或其他空格字符),并且只关心第一个空格前的所有内容和第一个空格后的所有内容,那么您可以不使用如下的正则表达式:

str.substr(0,str.indexOf(' ')); // "72"
str.substr(str.indexOf(' ')+1); // "tocirah sneab"

Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).

注意,如果根本没有空格,那么第一行将返回一个空字符串,第二行将返回整个字符串。确保这是你在这种情况下想要的行为(或者这种情况不会出现)。

#2


34  

Javascript doesn't support lookbehinds, so split is not possible. match works:

Javascript不支持后来居上,所以拆分是不可能的。匹配工作原理:

str.match(/^(\S+)\s(.*)/).slice(1)

Another trick:

另一个技巧:

str.replace(/\s+/, '\x01').split('\x01')

how about:

如何:

[str.replace(/\s.*/, ''), str.replace(/\S+\s/, '')]

and why not

为什么不

reverse = function (s) { return s.split('').reverse().join('') }
reverse(str).split(/\s(?=\S+$)/).reverse().map(reverse)

or maybe

或者

re = /^\S+\s|.*/g;
[].concat.call(re.exec(str), re.exec(str))

#3


14  

In ES6 you can also

在ES6中你也可以

let [first, ...second] = str.split(" ")
second = second.join(" ")

#4


11  

var arr = [];             //new storage
str = str.split(' ');     //split by spaces
arr.push(str.shift());    //add the number
arr.push(str.join(' '));  //and the rest of the string

//arr is now:
["72","tocirah sneab"];

but i still think there is a faster way though.

但是我仍然认为有一个更快的方法。

#5


7  

You can also use .replace to only replace the first occurrence,

你也可以使用。replace只替换第一个出现,

​str = str.replace(' ','<br />');

Leaving out the /g.

离开/ g。

DEMO

演示

#6


5  

georg's solution is nice, but breaks if the string doesn't contain any whitespace. If your strings have a chance of not containing whitespace, it's safer to use .split and capturing groups like so:

georg的解决方案很好,但是如果字符串不包含任何空格,就会中断。如果您的字符串有可能不包含空格,那么使用.split和捕获组(如:

str_1 = str.split(/\s(.+)/)[0];  //everything before the first space
str_2 = str.split(/\s(.+)/)[1];  //everything after the first space

#7


4  

Late to the game, I know but there seems to be a very simple way to do this:

我知道这是一场迟来的比赛,但似乎有一种非常简单的方法:

var str = "72 tocirah sneab";
var arr = str.split(/ (.*)/);

This will leave arr[0] with "72" and arr[1] with "tocirah sneab".

这将留下arr[0]和“72”,arr[1]和“tocirah sneab”。

For reference:

供参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split Capturing_parentheses

#8


2  

Just split the string into an array and glue the parts you need together. This approach is very flexible, it works in many situations and it is easy to reason about. Plus you only need one function call.

只需将字符串分割成一个数组,然后将需要的部分粘在一起。这种方法非常灵活,在许多情况下都适用,而且很容易推理。加上你只需要一个函数调用。

arr = str.split(' ');             // ["72", "tocirah", "sneab"]
strA = arr[0];                    // "72"
strB = arr[1] + ' ' + arr[2];     // "tocirah sneab"

Alternatively, if you want to cherry-pick what you need directly from the string you could do something like this:

或者,如果你想直接从字符串中选择你需要的东西,你可以这样做:

strA = str.split(' ')[0];                    // "72";
strB = str.slice(strA.length + 1);           // "tocirah sneab"

Or like this:

或者像这样:

strA = str.split(' ')[0];                    // "72";
strB = str.split(' ').splice(1).join(' ');   // "tocirah sneab"

However I suggest the first example.

但是我建议第一个例子。

Working demo: jsbin

工作演示:jsbin

#9


1  

Whenever I need to get a class from a list of classes or a part of a class name or id, I always use split() then either get it specifically with the array index or, most often in my case, pop() to get the last element or shift() to get the first.

每当我需要得到一个类从一个列表的类或类名称或id的一部分,我总是使用分割()然后把它与数组索引或特别,常常在我的例子中,pop()来获得最后一个元素或转移()来获得第一。

This example gets the div's classes "gallery_148 ui-sortable" and returns the gallery id 148.

这个示例获取div的类“gallery_148 ui-sortable”并返回gallery id 148。

var galleryClass = $(this).parent().prop("class"); // = gallery_148 ui-sortable
var galleryID = galleryClass.split(" ").shift(); // = gallery_148
galleryID = galleryID.split("_").pop(); // = 148
//or
galleryID = galleryID.substring(8); // = 148 also, but less versatile 

I'm sure it could be compacted into less lines but I left it expanded for readability.

我确信它可以被压缩成更少的行,但我保留了它的可读性。

#10


0  

I needed a slightly different result.

我需要一个稍微不同的结果。

I wanted the first word, and what ever came after it - even if it was blank.

我想要的是第一个词,以及它之后的一切——即使它是空白的。

str.substr(0, text.indexOf(' ') == -1 ? text.length : text.indexOf(' '));
str.substr(text.indexOf(' ') == -1 ? text.length : text.indexOf(' ') + 1);

so if the input is oneword you get oneword and ''.

所以如果输入是一个单词,你得到一个单词。

If the input is one word and some more you get one and word and some more.

如果输入是一个单词和更多的单词,你会得到一个单词和更多的单词。