Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  91] [ 2]  / answers: 1 / hits: 59493  / 13 Years ago, thu, may 12, 2011, 12:00:00

Time and time again I see $1 and $2 being used in code. What does it mean? Can you please include examples?


More From » javascript

 Answers
4

When you create a regular expression you have the option of capturing portions of the match and saving them as placeholders. They are numbered starting at $1.



For instance:



/A(d+)B(d+)C/


This will capture from A90B3C the values 90 and 3. If you need to group things but don't want to capture them, use the (?:...) version instead of (...).



The numbers start from left to right in the order the brackets are open. That means:



/A((d+)B)(d+)C/


Matching against the same string will capture 90B, 90 and 3.


[#92270] Wednesday, May 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mahogany

Total Points: 645
Total Questions: 107
Total Answers: 98

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;