split多个分隔符进行分割

那个是源自百度IFE的题目:

单行变成多行输入框,一个按钮,输入框中用来输入用户的兴趣爱好,允许用户用换行、空格(全角/半角)、逗号(全角/半角)、顿号、分号来作为不同爱好的分隔。

然后是我的解决方案:

还是要用正则表达式,结果正确的,如果有更好的方法记得告诉我呀~~~

var str=$("#text").val();
var strs = str.split(/[, , 、 ; \s+]/);
alert(strs.length);

整题是对输入信息进行分割,去空,去重,我的代码是酱紫的,相互学习~~~

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#submit").click(function(){
                var str=$("#text").val();
                var strs = str.split(/[, , 、 ; \s+]/);
                alert(strs.length);
                console.log(strs);

                var newStrs = [];
                var json ={};
                for(i=0;i<strs.length;i++){
                    if(strs[i]){
                        if(!json[strs[i]]){
                            newStrs.push(strs[i]);
                            json[strs[i]]=1;
                        }
                    }
                }

            })

        })
    </script>
</head>
<body>
    <div>stage 2</div>
    <textarea type="text" style="width:400px;" id="text"></textarea>
    <input type="button" value="提交" id="submit" />
    <div style="border:1px"></div>

</body>
</html>
时间: 2024-08-08 18:54:22

split多个分隔符进行分割的相关文章

awk按照多个分隔符进行分割

我们知道awk可以进行类似于cut之类的操作,如一个文件data如下 zhc-123|zhang hongchangfirst-99|zhang hongchang-100|zhang 如果我们 awk -F '-' '{print $1;}' data 会打印出 zhc hongchangfirst hongchang 但是如果我想根据多个分隔符进行分割呢?一种办法是两次awk,但是我们可以一次告诉awk我们所有的分隔符,如-和|这两个,如 awk -F '[-|]' '{print $3;}

jquery split 多个分隔符

如下: 如果我想 对jsp页面中  一个字符窜按照多个分隔符 进行分隔 需要如下进行: var str = "主治疾病,疾病,病."; var arr = str.split(/[, ..,]/);

[LeetCode] Split Linked List in Parts 分割链表成部分

Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts". The length of each part should be as equal as possible: no two parts should have a size differing by more than 1.

[LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

Python split()方法分割字符串

Python 中,除了可以使用一些内建函数获取字符串的相关信息外(例如 len() 函数获取字符串长度),字符串类型本身也拥有一些方法供我们使用. 注意,这里所说的方法,指的是字符串类型 str 本身所提供的,由于涉及到类和对象的知识,初学者不必深究,只需要知道方法的具体用法即可. split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串,这些子串会被保存到列表中(不包含分隔符),作为方法的返回值反馈回来.该方法的基本语法格式如下: str.split(sep,maxsplit)

Java String类的常用方法

String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. String(Sting original):构造一个original的副本.即:拷贝一个original. String(StringBuffer buffer):通过StringBuffer数组构造字符串对象. byte[] b = {'a','b','c','d','e','f','g','h','i','j'}; char[] c =

【转载】Java中String类的方法及说明

转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一.构造函数     String(byte[ ] bytes):通过byte数组构造字符串对象.     String(char[ ] value):通过char数组构造字符串对象.     String(Sting original):构造一个original的副本.即:拷贝一个original.     String(Strin

java基础知识回顾之---java String final类普通方法

辞职了,最近一段时间在找工作,把在大二的时候学习java基础知识回顾下,拿出来跟大家分享,如果有问题,欢迎大家的指正. /*     * 按照面向对象的思想对字符串进行功能分类.     *      *      * 1,获取:     * 1.1 获取字符串中字符的个数(长度).     *         int length();     * 1.2 取字符串中的某一个字符,其中的参数index指的是字符串中序数.字符串的序数从0开始到length()-1 .     *