6

Here is the case:

var stringExample = "hello=goodbye==hello";
var parts = stringExample.split("=");

Output:

hello,goodbye,,hello

I need this Output:

hello,goodbye==hello

Contiguous / repeated characters must be ignored, just take the single "=" to split.

Maybe some regex?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Martín
  • 3,105
  • 7
  • 25
  • 43
  • 1
    Will there always be alphanumeric characters around the `=`s that you do want to split on? Or could there be something like `hello:=!goodbye` that should be split into `hello:` and `!goodbye`? – Tim Pietzcker Jan 08 '13 at 14:57

3 Answers3

6

You can use a regex :

var parts = stringExample.split(/\b=\b/);

\b checks for word boundaries.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
3

Most probably, @dystroys answer is the one you're looking for. But if any characters other than alphanumerics (A-Z, a-z, 0-9 or _) could surround a "splitting ="), then his solution won't work. For example, the string

It's=risqué=to=use =Unicode!=See?

would be split into

"It's", "risqué=to", "use Unicode!=See?"

So if you need to avoid that, you would normally use a lookbehind assertion:

result = subject.split(/(?<!=)=(?!=)/);  // but that doesn't work in JavaScript!

So even though this would only split on single =s, you can't use it because JavaScript doesn't support the (?<!...) lookbehind assertion.

Fortunately, you can always transform a split() operation into a global match() operation by matching everything that's allowed between delimiters:

result = subject.match(/(?:={2,}|[^=])*/g);

will give you

"It's", "risqué", "to", "use ", "Unicode!", "See?"
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • +1 I was wondering why the lookbehind in the `split` I was testing wasn't working. Is the "doesn't work in Javascript!" documented somewhere ? – Denys Séguret Jan 08 '13 at 15:06
  • @dystroy: http://www.regular-expressions.info/refflavors.html is my go-to resource for this (or RegexBuddy). – Tim Pietzcker Jan 08 '13 at 15:14
  • 2
    `match` is usually not equivalent to a `split` in the way it handles strings beginning/ending with delimiters. Also, it could return `null`. The transformation is not trivial :-) And at least you would need to use `*` instead of `+` – Bergi Jan 08 '13 at 15:18
  • @Bergi: Right, `*` it is. But I think now the result is equivalent to a (fictitious) split on the regex I mentioned above. – Tim Pietzcker Jan 08 '13 at 15:22
-1

As first approximation to a possible solution could be:

".*[^=]=[^=].*"

Note that this is just the regex, if you want to use it with egrep, sed, java regex or whatever, take care if something needs to be escaped.

BEWARE!: This is a first approximation, this could be improved. Note that, for instance, this regex won't match this string "=" (null - equal - null).

Ole
  • 366
  • 1
  • 3
  • 13
  • Can you precise how this regex should be used in OP's case ? – Denys Séguret Jan 08 '13 at 16:30
  • @dystroy `precise` isn't a verb, but looking at your profile I see you must speak French. – jackcogdill Jan 08 '13 at 22:51
  • @dystroy I don't understand you. What OP's means? If you are asking for an example of how to use that regex in/with a program let's see it. The content of a file: hello=world hell0==w0rld bye=cruel==world by3==cru3l=w0rld = == === – Ole Jan 09 '13 at 08:21
  • OP wants to split a string. Can you produce, using your regex, the needed javascript code or was your answer really just a comment ? – Denys Séguret Jan 09 '13 at 08:23
  • Ok, now I realize I cannot use code blocks inside comments ¬¬. Also I have spent my 5 minutes to change the comment. Explain what you are trying to ask and I will answer you :) – Ole Jan 09 '13 at 08:28
  • a) This regex does nothing more than match an entire string that contains at least one `=` with at least one non-`=` character before and after the `=`. It doesn't do anything that would help with splitting that text into partitions. b) Even if you used capturing groups to capture the parts before and after the `=`, you could only split a string into two parts. As soon as there's more than one splitting `=`, the regex will fail. – Tim Pietzcker Jan 09 '13 at 09:16