0

I have string like this:

prikey = 2, ju = 20150101, name = sdf, email = sdfsdf@naver.com, sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용

I wanna split each pair like this:

  1. prikey=2

  2. ju=20150101

  3. name=sdf

  4. email=sdfsdf@naver.com

  5. sub=(한진해운) 2014년도 케미컬 선장, 1항기, 사 채용

I tried this code:

/((?:[^=,]+)=(?:[^=]+)),/g

But it doesn't work fine.

  1. prikey=2

  2. ju=20150101

  3. name=sdf

  4. email=sdfsdf@naver.com

  5. sub=(한진해운) 2014년도 케미컬 선장

Community
  • 1
  • 1
Luc
  • 2,800
  • 2
  • 25
  • 46

2 Answers2

2

You'll likely be able to capture what you want with a pattern such as:

(?:,)\s|([^=]+=\s[\w@\.\s]+|[\w].+)

Result:

prikey = 2 
ju = 20150101 
name = sdf
email = sdfsdf@naver.com
sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용

Example:

https://regex101.com/r/sP5sB8/1

Code:

http://jsfiddle.net/df06waLd/

l'L'l
  • 44,951
  • 10
  • 95
  • 146
0

You can just use str.split(', ')

var str = 'prikey = 2, ju = 20150101, name = sdf, email = sdfsdf@naver.com, sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용';

var result = str.split(', ');
var wanted = [];

result.forEach(function(el) {
  if (el.indexOf('=') !== -1) {
    wanted.push(el);
  } else {
    wanted[wanted.length - 1] += ', ' + el;
  }
})

console.log(wanted);

And you will get:

["prikey = 2", "ju = 20150101", "name = sdf", "email = sdfsdf@naver.com", "sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용"]
iplus26
  • 2,518
  • 15
  • 26
  • who voted down leave a comment to say some reason please. :) – iplus26 Aug 04 '15 at 02:56
  • `result[4] == sub = (한진해운) 2014년도 케미컬 선장, 사 채용, 1항기` which is not what OP want. And this answer depends on the number of `,`s in the last item. – falsetru Aug 04 '15 at 02:57
  • @JackDuong Update the answer. How about this one? – iplus26 Aug 04 '15 at 03:16
  • @falsetru Sorry for my mistakes. I updated the answer and now the result depends on whether the string contains of a '='. I suppose that regular expression may work but it would be complicate, so I want to use a programming way... – iplus26 Aug 04 '15 at 03:25