用逗号分割字符串,用钱问题

时间:2017-11-12 21:23:15

标签: javascript regex

我有下面的字符串,我想用逗号分隔。但这个钱也存在问题,其中也有逗号。 我想要实现的目标;如果逗号后面有一个数字,请不要与该数字分开。我想用javascript做这个工作。

  

025056-03110,245056030,1,标准折扣,标准折扣, 15,940.00USD ,29/11 / 2017,1,

到目前为止我用千种组合尝试了什么;

  

[^ \,!\?] +

我期待的结果;

025056-03110
245056030
1
Standart Discount
Standart Discount
*15,940.00USD*
29/11/2017
1

4 个答案:

答案 0 :(得分:1)

此处的解决方案是将.match() this regex /(\d+,\d{3}[.]\d+\w+)|(\d+[\-][\d\w]+)|([\/\d\s\w]+)/g 一起使用,它将拆分所有元素并跳过带逗号的数字。

这应该是你的代码:

var str = "025056-03110,245056030,1,Standart Discount,Standart Discount,15,940.00USD,29/11/2017,1";
var matches = str.match(/(\d+,\d{3}[.]\d+\w+)|(\d+[\-][\d\w]+)|([\/\d\s\w]+)/g);

<强>演示:

var str = "025056-03110,245056030,1,Standart Discount,Standart Discount,15,940.00USD,29/11/2017,1";
var matches = str.match(/(\d+,\d{3}[.]\d+\w+)|(\d+[\-][\d\w]+)|([\/\d\s\w]+)/g);
console.log(matches);

答案 1 :(得分:0)

let str = '025056-03110,245056030,1,Standart Discount,Standart Discount,15,940.00USD,29/11/2017,1,';
let price = str.match(/[A-Za-z],(\d+,?\d{1,3}\.\d{2}\w{3})/)[1];
let temp = price.replace(',', '|');

console.log(
  str.replace(price, temp)
    .split(',')
    .map(v => v.replace('|', ','))
    .filter(v => v)
);

答案 2 :(得分:0)

也许你可以这样做:

(?:\d+,(?=\d+\.).+?(?=,)|[\w-\/.\s]+)

&#13;
&#13;
var pattern = /(?:\d+,(?=\d+\.).+?(?=,)|[\w-\/.\s]+)/g;
var str = "025056-03110,245056030,1,Standart Discount,Standart Discount,15,940.00USD,29/11/2017,1,";
var matches = str.match(pattern);
for (var i = 0; i < matches.length; i++) {
    if (matches[i].indexOf('USD') !== -1) {
        matches[i] = "*" + matches[i] + "*";
    }
}
console.log(matches.join('\n'));
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

而是match替代方案:\*[^*]*\*|[^\s,][^,]*

{{1}}