首页 新闻 赞助 找找看

js 数组拆分

0
悬赏园豆:50 [已解决问题] 解决于 2018-06-07 17:58

麻烦大神们如何拆分

var str2 ="“你俩抬前头,我抬后头吧!”";
将str拆分成数组[ "“","你","俩","抬","前","头",",","”","!”" ]

雨慢慢下的主页 雨慢慢下 | 初学一级 | 园豆:3
提问于:2018-06-07 10:45
< >
分享
最佳答案
0

我就想知道,你怎么把逗号面 我抬后头吧 给弄没了?

换个问法 var str2 = "“我吃汉堡,你吃面条,他吃炒饭!”"; 这句话,按照你的预期的分割方式,结果应该是什么样?

收获园豆:50
写代码的相声演员 | 小虾三级 |园豆:517 | 2018-06-07 15:12

效果要做成这样。麻烦了。

雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 15:17

@雨慢慢下: 

那之前那两个哥们的回答没问题啊

代码

console.log("“我吃汉堡,你吃面条,他吃炒饭!”".split(''))

以上代码等价与下面的代码

var str2 = "“我吃汉堡,你吃面条,他吃炒饭!”";
var result = str2.split(''); //这个result就是你要的结果

//之后随便你怎么做,假如我们来控制台输出一下
console.log(result)

 

写代码的相声演员 | 园豆:517 (小虾三级) | 2018-06-07 15:37

@写代码的相声演员: 这样分割,字符串中的每个元素都变成数组元素了,最后一个两个标点符号是要放在一起,作为一个元素的。

雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 15:41

@雨慢慢下: 

那就遍历一下啊...判断一下是不是标点...然后整合数组

你确定是需求是,只让最后两个符号组成一个元素么?

还是,把末尾所有的符号组成一个元素?

还是,把所有连续的符号组成一个元素?

写代码的相声演员 | 园豆:517 (小虾三级) | 2018-06-07 15:47

@写代码的相声演员: 只要两个连续的标点符号,组成一个元素。代码想了好久,写不出来啊。大神,帮帮忙

雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 15:48
 1 var mysplit = function(str) {
 2 
 3     //Split the string first
 4     var arr = str.split('');
 5 
 6     //Add a placeholder, the content is not important, mainly to let the following loop trigger a judgment at the end
 7     arr.push(""); 
 8 
 9     var point = 0; //Pointer for the circular
10     var start = -1; //Start sign, default -1
11 
12     //This is a regular expression that matches Chinese characters
13     //It can match 。 ? ! , 、 ; : “ ” ‘ ' ( ) 《 》 〈 〉 【 】 『 』 「 」 ﹃ ﹄ 〔 〕 … — ~ ﹏ ¥
14     const pattern = /[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/;
15 
16     while (point < arr.length) {
17         if (pattern.test(arr[point])) { 
18             //Determine if the current element is a punctuation
19             if (start < 0) {       
20                 //Record start position
21                 start = point;
22             }
23         } else if (start > -1) {    
24             //Only enters here if the current character is not a punctuation mark and it marks the starting position
25             if ((point - start) <= 1) {
26                 //If the current point has only one length from the starting point, it means that the symbols appear alone, not consecutively, clearing the start marker
27                 start = -1;     
28             } else {
29                 //Remove the element from the start point to the pointer before taking it from the array and occupy it with an empty string
30                 var group = arr.splice(start, point - start, "");
31 
32                 //Splicing the retrieved elements into a new string and replacing placeholders
33                 arr[start] = group.join('');
34 
35                 //Reset pointer and starting point position
36                 point = start;
37                 start = -1;
38 pattern 
39         }
40         //Pointer down
41         point++;
42     }
43     //Remove the last placeholder
44     arr.splice(arr.length - 1, 1)
45 
46     //return the result
47     return arr;
48 }

就说两点

JS的正则没法直接匹配所有的标点符号,所以用 pattern 做了一个正则枚举了一大堆中文的标点符号,如果不够用Unicode详细分类 自行添加

另外,这个代码仅适配中文标点,如果需要适配英文字符,方法有两个,一个式增加 pattern 适配的元素,二是做一个英转中文字符的转化方法,在循环的第一个if中先转化再test。

 

其他的注释里都写了,个别方法看不懂,请自行百度,w3school

写代码的相声演员 | 园豆:517 (小虾三级) | 2018-06-07 17:11

@写代码的相声演员:  谢谢,解决了。

雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 17:58
其他回答(3)
0

var result=[];

for(int i=0; i<str2.length; i++){

result.push(str2[i])

}

console.log(result)

carsonHua | 园豆:413 (菜鸟二级) | 2018-06-07 11:07

谢谢你的回答。可是,最终的效果要的是这样 [”,你,俩,抬,前,头,!”]

支持(0) 反对(0) 雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 11:11
0

var str2 = "“你俩抬前头,我抬后头吧!”";
ss1 = str2.split('');
document.write('ss1'+ss1);

 

不知道是不是你需要的

青青子衿619 | 园豆:300 (菜鸟二级) | 2018-06-07 11:37

最终的效果是这样

支持(0) 反对(0) 雨慢慢下 | 园豆:3 (初学一级) | 2018-06-07 11:42
0

var str = "“你俩抬前头,我抬后头吧!”";
var result = str.substring(0, str.length-2).split('');
result.push(str.substring(str.length -2));
document.write('result='+result);

、熙和 | 园豆:1508 (小虾三级) | 2018-06-07 17:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册