正则表达式查找逗号后跟非空白字符 [英] Regex to find comma followed by non-whitespace characters

查看:58
本文介绍了正则表达式查找逗号后跟非空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JS split()函数对逗号后面的非空格进行拆分,而忽略后面带有任何空格的逗号.

I'm trying to use the JS split() function to split on commas that are followed by non-whitespace, while ignoring commas with any whitespace after.

例如,字符串"1、2、3"根本不应拆分,而字符串"1、2、3"应拆分为:

For example, the string "one, two, three", should not be split at all, while "one,two, three" should be split into:

  • 一个
  • 二,三

我尝试使用 .split(',\\ S') .split(',(?= \\ S))')和其他变体,但并没有让它以我想要的方式分裂.

I've tried using .split(',\\S') .split(',(?=\\S)")') and other variations, but haven't had any luck with getting it to split the way I want.

推荐答案

与正则表达式一起使用

str.split(/,(?=\S)/)

或解析正则表达式字符串以进行转换

or parse the regex string to convert

str.split(new RegExp(',(?=\\S)'))

var str = 'a,b,c, d,e, f';

console.log(
  str.split(/,(?=\S)/)
);
console.log(
  str.split(new RegExp(',(?=\\S)'))
);

这篇关于正则表达式查找逗号后跟非空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆