简体   繁体   English

使用正则表达式分割字符串

[英]Using regular expression to split a string

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 . 我只需要获取viewframesizeheight 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. 我是Java语言的新手,所以也许我想要不可能的事,让我知道。 Thanks. 谢谢。

In Javascript, executing a regexp with the g modifier doesn't return all the matches at once. 在Javascript中,使用g修饰符执行regexp不会一次返回所有匹配项。 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 . .*是贪婪的,因此它返回可能的最长匹配项,因此它返回的是first和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. height之后,因此最后一个单词将不匹配它。

EDIT: I've changed the regexp to use \\w* instead of [^.]* , because it was grabbing the whole height = 44 string instead of just height . 编辑:我已将正则表达式更改为使用\\w*而不是[^.]* ,因为它捕获的是整个height = 44字符串,而不仅仅是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] + ">");
}

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);
}

In your context, split is a MUCH better option: 在您的上下文中, split是一个更好的选择:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM