Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search.
If I input
$1
, the command line says it is not defined; if I input
"$1"
, the command line only echoes
$1
, not 11222. So, how does the replace method know what
"$1,$2"
mean?
Thank you.
–
–
It's not a "variable" - it's a placeholder that is used in the
.replace()
call.
$n
represents the
nth
capture group of the regular expression.
var num = "11222333";
// This regex captures the last 3 digits as capture group #2
// and all preceding digits as capture group #1
var re = /(\d+)(\d{3})/;
console.log(re.test(num));
// This replace call replaces the match of the regex (which happens
// to match everything) with the first capture group ($1) followed by
// a comma, followed by the second capture group ($2)
console.log(num.replace(re, "$1,$2"));
–
–
–
$1 is the first group from your regular expression, $2 is the second. Groups are defined by brackets, so your first group ($1) is whatever is matched by (\d+). You'll need to do some reading up on regular expressions to understand what that matches.
It is known that in Javascript, the name of variables should begin with letter or _, how can $1 be a valid name of member variable of RegExp here?
This isn't true. $ is a valid variable name as is $1. You can find this out just by trying it. See jQuery and numerous other frameworks.
You are misinterpreting that line of code. You should consider the string
"$1,$2"
a
format specifier
that is used internally by the
replace
function to know what to do. It uses the previously tested regular expression, which yielded 2 results (two parenthesized blocks), and reformats the results.
$1
refers to the first match,
$2
to the second one. The expected contents of the
num
string is thus
11222,333
after this bit of code.
No, it's not.
$1
is a perfectly valid variable. You have to assign to it first though:
$variable = "this is a test"
This is how jQuery users a variable called $
as an alias for the jQuery
object.
This book is made of paper. And paper cannot oppose any resistance to whom is writing on it :-) . But perhaps did you only misinterpret what is actually written in this book.
Actually, it is depending on the context.
In the context of the replace()
method of String
, $1
, $2
, ... $99
(1 through 99) are placeholders. They are handled internally by the replace()
method (and they have nothing to do with RegExp.$1
, RegExp.$2
, etc, which are probably not even defined (see point 2. )). See String.prototype.replace() #Specifying_a_string_as_a_parameter. Compare this with the return value of the match()
method of String
when the flag g
is not used, which is similar to the return value of the exec()
method of RegExp
. Compare also with the arguments passed implicitly to an (optional) function specified as second argument of replace()
.
RegExp.$1
, RegExp.$2
, ... RegExp.$9
(1 through 9 only) are non-standard properties of RegExp
as you may see at RegExp.$1-$9 and Deprecated and obsolete features. They seem to be implemented on your browser, but, for somebody else, they could be not defined. To use them, you need always to prepend $1
, $2
, etc with RegExp.
. These properties are static, read-only and stored in the RegExp
global object, not in an individual regular expression object. But, anyway, you should not use them. The $1
through $99
used internally by the replace()
method of String
are stored elsewhere.
Have a nice day!
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.