1

I have a string which I need to separate correctly:

self.view.frame.size.height = 44

I need to get only view, frame, size, and height. And I need to do it with a regular expression. So far I've tried a lot of variants, none of them are even close to what I want to get. And my code now looks like this:

var testString = 'self.view.frame.size.height = 44'
var re = new RegExp('\\.(.*)\\.', "g")
var array = re.exec(testString);
console.log('Array length is ' + array.length)
for (var i = 0; i < array.length; i++) {
    console.log('<' + array[i] + ">");
}

And it doesn't work at all:

Array length is 2
<.view.frame.size.>
<view.frame.size>

I'm new at Javascript, so maybe I want the impossible, let me know. Thanks.

4
  • 2
    Why do you have to do it with a regular expression? What's wrong with String.prototype.split()? Commented Aug 16, 2014 at 0:16
  • You need to use a non-greedy quantifier, or use ([^.]*). Commented Aug 16, 2014 at 0:17
  • Re: result, are you looking for a string: 'view.frame.size.height', or an array: ['view', 'frame', 'size', 'height'] ? Commented Aug 16, 2014 at 2:59
  • I'm looking for an array. Commented Aug 16, 2014 at 11:40

3 Answers 3

3

In Javascript, executing a regexp with the g modifier doesn't return all the matches at once. You have to execute it repeatedly on the same input string, and each one returns the next match.

You also need to change the regexp so it only returns one word at a time. .* is greedy, so it returns the longest possible match, so it was returning all the words between the first and last .. [^.]* will match a sequence of non-dot characters, so it will just return one word. You can't include the second . in the regexp, because that will interfere with the repetition -- each repetition starts searching after the end of the previous match, and there's no beginning . after the ending . of the word. Also, there's no . after height, so the last word won't match it.

EDIT: I've changed the regexp to use \w* instead of [^.]*, because it was grabbing the whole height = 44 string instead of just height.

var testString = 'self.view.frame.size.height = 44';
var re = /\.(\w*)/g;
var array = [];
var result;
while (result = re.exec(testString)) {
    array.push(result[1]);
}
console.log('Array length is ' + array.length)
for (var i = 0; i < array.length; i++) {
    console.log('<' + array[i] + ">");
}
Sign up to request clarification or add additional context in comments.

3 Comments

I just had the parenthesis in the wrong place, I fixed it.
Well, seems like I can't get what I want with regex. Alright, thank you anyway, nice answer, now I have better understanding of how it works.
Just a little new question. Is there a way to get only bounds in UIScreen.mainScreen().bounds? I mean I need only these ones that don't have parentheses at the end.
1

If you're sure that your data will be always in the same format you can use this:

function parse (string) {
    return string.split(" = ").shift().split(".").splice(1);
}

Comments

0

In your context, split is a MUCH better option:

var str = "self.view.frame.size.height = 44";
var bits1 = str.split(" ")[0];
var bits2 = bits1.split(".");
bits2.shift(); // get rid of the unwanted self
console.log(bits2);

1 Comment

Sorry, @JeremyJStarcher, you're right. bits2.shift() returns "self" but the value of bits2 then is the expected result.

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.