4

The sample string is =aa=bb=cc=dd=.

I tried

string.match(/=(\w*)=/)

but that returns only aa.

How do I find aa, bb, cc and dd from the sample string?

3
  • 8
    Note that you could also simply split the string :) Commented Jul 8, 2013 at 14:41
  • @WoLpH, how would it be possible to split the string without either an extra = in each item or an empty item starting or finishing the array? Commented Jul 8, 2013 at 15:14
  • @DanielGimenez - it would, but you could very easily skip values that were blank when processing the array. Commented Jul 8, 2013 at 16:00

5 Answers 5

4

This regex will match explicitly your requirements, and put the non, delimiter portion it the first capture group:

=([^=]+)(?==)

Unfortunately JavaScript regex does not have look behinds, otherwise this could be done in much easier fashion.

Here is some code:

var str = '=([^=]+)(?==)';

var re = /=([^=]+)(?==)/g,
    ary = [],
    match;

while (match = re.exec(str)) {
    ary.push(match[1]);
}

console.log(ary);
Sign up to request clarification or add additional context in comments.

3 Comments

What is the (?==) capturing group for? Without it, it works fine too. Can you please post a string where this is important?
@learner, without the look ahead, then nomatch in =a=b=nomatch,=c= would match. Just trying to be as exact as possible.
@learner What Daniel means is that (?= ... ) is a lookahead. It checks that the character coming just after is what is within the ... without consuming it, so that you don't get alternate results (i.e. aa, cc and missing bb, dd).
2

var values = yourString.split('='); You'll get an array with all your required values.

4 Comments

Deleting an answer to rid yourself of the downvotes and then reposting a corrected answer is strongly discouraged. See here and here
Because my answer just had to be corrected a little bit, and now it's correct. I don't think that the correct answer should have downvotes :)
So I make a little mistake, and others start to downvote and laugh at me in the comments instead of helping me to correct it. A very kind society I'd say :)
A little teasing - it's not personal. Please consider, though, that the purpose of this site is not to hold your hand, but to provide high quality answers for the rest of the world. Posting incorrect or misleading answers is anathema to that mission and is going to be met with the appropriate response - downvotes and comments pointing out the error. You've corrected your mistake and provided a decent answer, which is more than most newcomers would do. +1 for that.
2

I believe /[^=]+/g should suit your needs: have a try!

Comments

1

Just use simply:

string.split('=')

Comments

1

Description

this regex will capture all the non-equal characters in your string

=([^=]*)(?==)

enter image description here

Example

Live example: http://www.rubular.com/r/vPW2GJqBWP

Sample Text

=aa=bb=cc=dd=

Matches

Capture group 1 will have the following

[0] => aa
[1] => bb
[2] => cc
[3] => dd

11 Comments

Why did this get down voted? It is the correct regex. Though now it is pretty much the same answer I gave...
@DanielGimenez I didn't vote it down, but I think it's the fact that without a loop, the regex will not work properly (and the loop was added later, after which one vote was reversed).
people are playing games artificially inflating their answers by downvoting others
@Denomales: you forgot the global flag (and I got down voted too).
@Denomales - You are still missing the global flag. Your example is in ruby, not javascript. This answer will not work in javascript without the global flag.
|

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.