I need to split a string to one or more substrings each of which contains no more or less than two dots. For example, if the string is foo.boo.coo.too"
then what would be the regex to get the following array?: ["foo.boo.coo", "boo.coo.too"]
. I hope there will be someone to answer this question - I will really admire you, as I've been programming for several years and have not still be used to regular expressions well enough to solve this particular problem by myself. Thank you very much in advance. Let me know your identity so that I can credit you as a contributor of the program I am creating.

- 253
- 1
- 3
- 7
-
This has nothing to do with javascript perse but http://www.rubular.com/ is a very good tool for learning the basics of regexp. – Henrik Andersson Mar 21 '13 at 09:03
-
@limelights, are you telling me that this is impossible to accomplish with regex? Why not? – Bob Torrent Mar 21 '13 at 09:07
-
@limelights, or what do you mean by "javascript perse"? – Bob Torrent Mar 21 '13 at 09:09
-
http://www.thefreedictionary.com/per+se – mplungjan Mar 21 '13 at 09:14
-
Someone should be able to fix this: http://jsfiddle.net/mplungjan/AhcTG/ – mplungjan Mar 21 '13 at 09:14
-
I'm not sure that this is what the OP asked for. – Jan Groth Mar 21 '13 at 09:15
-
@limelights, of course, the whole question may not belong to JavaScript solely. – Bob Torrent Mar 21 '13 at 09:21
-
@mplungjan, your regex doesn't work in my case, but if that is what meant by you and "someone should be able to fix it" then ok, thank you :) – Bob Torrent Mar 21 '13 at 09:37
-
@BobTorrent i translated directly from my native language, sorry about that. I was referring to the tool itself, as it's more aimed towards Ruby than javascript! :) Sorry for the confusion, it was entirely my fault! – Henrik Andersson Mar 21 '13 at 09:46
-
@limelights, don't be sorry :) And, by the way, my native language is JavaScript. But those regexes are always hard to me. – Bob Torrent Mar 21 '13 at 09:50
-
@BobTorrent I meant swedish ;) – Henrik Andersson Mar 21 '13 at 09:59
-
@limelights, Swedish is great! My mother tongue is Russian ;) – Bob Torrent Mar 21 '13 at 10:04
-
Yes, it meant that my regex is only getting the first occurrence and I was hoping someone would care to fix it – mplungjan Mar 21 '13 at 11:09
2 Answers
Regex by its nature will return non-intersecting results, so if you want "all matches" from a single regex - it's not possible.
So basically you will need to find first match, and then start from next position to find next match and so on; something like this technique described here regex matches with intersection in C# (it's not JavaScript but idea is the same)
You can use the following regex for example:
(?<=^|\.)((?:[^.]*\.){2}[^.]*?)(?=$|\.)
It ensures that it starts and ends with dot, or at begin/end of line, and contains exactly two dots inside, and captures result in first capture. You can replace * with + to make sure at least one symbol exists between dots, if it is required.
But you need to understand that such approach has really bad performance for the task you are solving, so may be using other way (like split + for) will be better solution.
-
Thank you! I understand the idea behind the non-intersecting. But should the regex example you mentioned work in JavaScript? Can you please create a jsfiddle/jsbin demo? – Bob Torrent Mar 21 '13 at 11:54
-
And as for the bad performance, why is it really that bad for solving my task? – Bob Torrent Mar 21 '13 at 12:01
RegEx is for this Problem not the best solution a similar problem was discussed here: split-a-sting-every-3-characters-from-back-javascript
A good javascript solution would be a javascript function like this
function splitter(text){
var parts = text.split(".");
var times = parts.length - 2;
var values = [];
for(var index = 0; index<times;index++)
{
values.push(parts.slice(index,index+3).join("."));
}
return values;
}
splitter("too.boo.coo.too")
//=> Result tested on Chrome 25+ ["too.boo.coo", "boo.coo.too"]
I hope this helps
If you want to Use Regex try the Lookhead Stuff, this could help http://www.regular-expressions.info/lookaround.html

- 1
- 1

- 12,173
- 4
- 36
- 61