简体   繁体   English

使用点表示法对变量进行JavaScript连续split()

[英]JavaScript successive split() on variables with dot notation

I have this variable: 我有这个变量:

var myVar = "r.object.subobject.variable";

I want to add each of the "levels" of this string to an array. 我想将此字符串的每个“级别”添加到数组中。 My array would contain: 我的数组将包含:

"r.object"
"r.object.subobject"
"r.object.subobject.variable"

I know I can use JavaScript's split() function with a RegEx, but the one I have keeps returning the whole string. 我知道我可以使用带有RegEx的JavaScript的split()函数,但是我一直在返回整个字符串。 For example, I'm trying: 例如,我正在尝试:

var myArray = myVar.split(/r[.].+/);

Thus, myArray[0] always comes out as "r.object.subobject.variable" . 因此, myArray[0]总是以"r.object.subobject.variable"

How can I make myArray[0] = "r.object" , myArray[1] = "r.object.subobject" , and myArray[2] = "r.object.subobject.variable" ? 如何使myArray[0] = "r.object"myArray[1] = "r.object.subobject"myArray[2] = "r.object.subobject.variable"

Hopefully I'm only misunderstanding how regular expressions work instead of needing to do some kind of contrived recursion or array concatenation by only separating on the periods. 希望我只是误解正则表达式是如何工作的,而不是仅通过分离句点来进行某种人为的递归或数组连接。

Additional Constraints and Background Information 附加约束和背景信息

  • The variables I'm checking may have a depth from 1 to an unknown number, so any function should try to determine how many strings to extract. 我正在检查的变量可能具有从1到未知数字的深度,因此任何函数都应该尝试确定要提取的字符串数。 All strings will always start with r. 所有字符串始终以r.开头r. (I have a while loop set up for this, and although it will correctly match for a given depth, the split still fails.) (我为此设置了一个while循环,虽然它会在给定深度上正确匹配,但拆分仍然失败。)
  • This operation will occur on thousands or tens of thousands of strings, so speed matters. 此操作将在数千或数万个字符串上发生,因此速度很重要。
  • I know bracket notation is a thing, but I cannot use it for my purposes. 我知道括号表示法是一个东西,但我不能将它用于我的目的。 (The array elements will eventually go back into a specialized AS3 application that doesn't always play nicely with it.) (数组元素最终将返回到一个专门的AS3应用程序,它并不总能很好地与它一起使用。)

Actually, myArray[0] should return an empty string. 实际上, myArray[0]应该返回一个空字符串。 The expression /r[.].+/ matches the whole string, thus returning everything after and before the match (empty strings). 表达式/r[.].+/匹配整个字符串,从而返回匹配之后和之前的所有内容(空字符串)。 Notice the .+ that matches any char repeated once or more times: 注意.+匹配任何重复一次或多次的char:

r.object.subobject.variable
--^                       ^      [1]: matched by r[.]
1 |           2           |
  +------------------------      [2]: matched my .+

If you want to split by the dots (" . "), use: 如果要按点(“ . ”)分割,请使用:

var myArray = myVar.split(/[.]/);

EDIT As for the updated question, regex can only match a part of the text once . 编辑至于更新的问题,正则表达式只能匹配文本的一部分一次

To achieve what you're trying to do, the following code should do: 要实现您要执行的操作,请执行以下代码:

 var myVar = "r.object.subobject.variable"; var mySplit = myVar.split("."); var myArray = Array(); for (i = 2; i <= mySplit.length; i++) { myArray.push(mySplit.slice(0,i).join(".")); } document.write(myArray); 

Try using .split() with RegExp /\\./ , do.. while loop , Array.prototype.slice() , Array.prototype.join() , Array.prototype.splice() 尝试使用.split()RegExp /\\./ do.. while循环, Array.prototype.slice()Array.prototype.join()Array.prototype.splice()

 var myVar = "r.object.subobject.variable"; var arr = myVar.split(/\\./), len = arr.length, i = 0, myArray = []; do { myArray.push(arr.slice(0, i).join(".")); ++i; } while (myArray.length <= len); myArray.splice(0, 2); console.log(myArray); 

I don't think you can do get a nice even break like that with regex, maybe if javasacript supported (?R) but doesn't seem like. 我不认为你可以用正则表达式得到一个很好的甚至休息,也许如果javasacript支持(?R)但看起来不像。 You could just split it by dots and then concatenate it. 你可以用点分割它然后连接它。

 function levels(str) { var levels; return str.split('.').reduce(function(acc, level, index) { if(index > 0) { levels = levels + '.' + level acc.push(levels); } else { levels = level; } return acc; }, []); } document.getElementById('results').innerHTML = JSON.stringify(levels('r.object.subobject.variable'), null, '\\t'); 
 <pre id="results"></pre> 

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

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