2

I'm using regex to get the indices of variables accessing pattern represented as string, for example:

string = "test[2][56].var1[4]"

The regex match result in the groups 2, 56, 4 captured.

matchGroups = ["2", "56", "4"]

The regex below works.

\[([^\.\]]+)\]

But I can't allow cases like:

"test.[3].var1" or "test[3]/"

I tried to limit the characters allowed before and after each group using the regex below:

[\]a-zA-Z0-9]\[([^\.\]]+)\]([\[a-zA-Z0-9])?

But some cases stopped working like the case "test[0].var7[3][4]"(4 is not captured).

I need help to make this work with all cases again(cases are in the link below).

https://regex101.com/r/mOgOHn/2

6
  • "But I can't allow cases like ... "test[3]/"": that puzzles me. So the / should make it not match? Which other characters should make it not match? What if "test[3][4]/"? Does then the [3] still match, but not the [4]? What about a space, a line break, a comma instead of the slash....etc? What should match in "test[1][hello][2][#][3], next[4]"? Commented Aug 1, 2022 at 4:17
  • Sorry @trincot I didn't specified it, but the text input should be a valid javascript array access pattern, test[3][4]/ is not valid in javascript for example. Commented Aug 1, 2022 at 16:33
  • So would you match test[foo[3]] and test["foo"] and test[4/2]? Commented Aug 1, 2022 at 17:29
  • Thank you for asking these questions I didn't clarify well, I should allow only numeric indexes like test[4], not expressions or object property, I also can't allow test[foo[3]], I didn't think about this case, I should avoid it too. Commented Aug 1, 2022 at 18:55
  • So if there is a mix of those, do you still want to match the numeric indexes, or should nothing match? Like for test[1].view[2]["foo"][3][4/2] should it match 1, 2 and 3, or should it match nothing? What if there is a semicolon after this expression, like test[1].view[2]["foo"][3][4/2]; ... should it then match nothing? Commented Aug 1, 2022 at 19:00

2 Answers 2

1

I would replace the string, then do matching

Replace Regex:

/.*?\[(\d+)\].*?/

Replace the string where any number between brackets is replaced by a number followed by ,

Match Regex:

/\d+(?=,)/

Look ahead positive; which Find any number followed by ,

const captureNumber = str => str.replace(/.*?\[(\d+)\].*?/g, "$1,").match(/\d+(?=,)/g)

console.log(captureNumber("test[0].var7[3][4]")); //[ "0", "3", "4" ]
console.log(captureNumber("test[3]/")); //[ "3" ] 
console.log(captureNumber("test.[3].var1")); //[ "3" ] 
console.log(captureNumber("test[2][56].var1[4]")); //[ "2", "56", "4" ]

Sign up to request clarification or add additional context in comments.

Comments

1

the simplest would be to use a regexp using a positive lookbehind and a positive lookahead. To this, you can add safety on spaces in case you have a case like "test[ 2 ][ 56 ].var1[ 4 ]", to which a .trim() method string must be applied. Here is a snippet illustrating this:

const getIndex = str => str.match(/(?<=\[) *\d+ *(?=\])/g).map(val => val.trim());

console.log(getIndex("test[0].var7[3][4]")); //[ "0", "3", "4" ]
console.log(getIndex("test[3]/")); //[ "3" ]
console.log(getIndex("test.[3].var1")); //[ "3" ]
console.log(getIndex("test[2 ][   56   ].var1[4 ]")); //[ "2", "56", "4" ]

Good luck !

Comments

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.