Node.js.

I have this regex ...

/user/([A-Za-z0-9]*) 

What matches this input line ...

 /user/me 

What brings this result to the console ...

 ['/user/me', 'me', index: 0, input: '/user/me'] 

Also see this example ...

 Regex: /user/([A-Za-z0-9]*)/([A-Za-z0-9]*) Input: /user/me/you Result: ['/user/me/you', 'me', 'you', index: 0, input: '/user/me/you'] 

The first result returns me , but the second returns me and you , is there a built-in function in node.js that will extract these occurrences or will I need to skip this array with conditions?

+4
source share
2 answers

Just use Array.slice :

 results=mystring.match(myregex).slice(1); 
+2
source

What version of Node are you using? The result of the match /user/me/you seems unfounded. Anyway, I am running the code below on Node.js 0.8.4

 > r = /\/user\/([A-Za-z0-9]*)/ /\/user\/([A-Za-z0-9]*)/ > r.exec('/user/me') [ '/user/me', 'me', index: 0, input: '/user/me' ] > r.exec('/user/me/you') [ '/user/me', 'me', index: 0, input: '/user/me/you' ] 
0
source

All Articles