【问题标题】:How to use jQuery Split with 2 Arguments?如何使用带有 2 个参数的 jQuery Split?
【发布时间】:2011-06-20 12:27:39
【问题描述】:

我想知道如何将jQuery.Split() 与“或”一起使用 - 即喜欢

example@example.com, example2@example.com,example3@example.com

所以它会像这样返回

example@example.com
example2@example.com
example3@example.com

但请注意 ",<space>""," - 也就是说 - 我想使用 ","",<space>" 来拆分这个字符串?

【问题讨论】:

  • 另一方面,split() 是一个 javascript 方法,与 jQuery 无关。

标签: jquery regex split


【解决方案1】:

没有 jQuery 拆分 ..

只需使用正则表达式..

var str = 'example@example.com, example2@example.com,example3@example.com';
var list = str.split(/,[ ]{0,}/);

或者正如@Andomar 在 cmets 中提到的那样,越简单

var list = str.split(/,\s*/);

【讨论】:

  • +1 单个字符不需要字符类,您可以使用* 作为{0,} 的简写(例如/,\s*/
【解决方案2】:

你可以使用正则表达式-

.split(new RegExp(pattern));

或者在你的情况下,可能我没有检查过这个并且对正则表达式相对较新。 \s 表示空格

.split(new RegExp('(\s*,\s*)'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多