0

I have string like this:

prikey = 2, ju = 20150101, name = sdf, email = [email protected], sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용

I wanna split each pair like this:

  1. prikey=2

  2. ju=20150101

  3. name=sdf

  4. [email protected]

  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 protected]

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

2

2 Answers 2

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 = [email protected]
sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용

Example:

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

Code:

http://jsfiddle.net/df06waLd/

Sign up to request clarification or add additional context in comments.

Comments

0

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

var str = 'prikey = 2, ju = 20150101, name = sdf, email = [email protected], 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 = [email protected]", "sub = (한진해운) 2014년도 케미컬 선장, 1항기, 사 채용"]

4 Comments

who voted down leave a comment to say some reason please. :)
result[4] == sub = (한진해운) 2014년도 케미컬 선장, 사 채용, 1항기 which is not what OP want. And this answer depends on the number of ,s in the last item.
@JackDuong Update the answer. How about this one?
@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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.