Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript replace method, replace with "$1"

I'm reading Sitepoints 2007 book "Simply Javascript" and I encountered some code I just can't understand.

It's the following code:

Core.removeClass = function(target, theClass) {     var pattern = new RegExp("(^| )" + theClass + "( |$)");     target.className = target.className.replace(pattern, "$1");     target.className = target.className.replace(/ $/, ""); }; 

The first call to the replace method is what puzzles me, I don't understand where the "$1" value comes from or what it means. I would think that the call should replace the found pattern with "".

like image 932
Niels Bom Avatar asked Jul 13 '10 09:07

Niels Bom


People also ask

What is $1 in replace JavaScript?

In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character". So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it.

What is $1 in regex replace?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

How do you replace a value in a string with another value?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

What is $1 jQuery?

$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. Show activity on this post. This is how jQuery users a variable called $ as an alias for the jQuery object.


1 Answers

Each pair of parentheses (...) where the first character is not a ?* is a "capturing group", which places its result into $1,$2,$3,etc which can be used in the replacement pattern.

You might also see the same thing as \1,\2,\3 in other regex engines, (or indeed in the original expression sometimes, for repetition)

These are called "backreferences", because they generally refer back to (an earlier) part of in the expression.

(*The ? indicates various forms of special behaviour, including a non-capturing group which is (?:...) and simply groups without capturing.)


In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character".

So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it. (The closing expression ( |$) is the inverse - a space or the string end position - and since its value isn't used, could have been non-capturing with (?: |$) instead.)


Hopefully this explains everything ok - let me know if you want any more info.

Also, here's some further reading from the site regular-expressions.info:

  • Groups and Backreferences
  • Atomic Grouping (doesn't work in JS, but interesting)
  • Lookaround groups (partial support in JS regex)
like image 190
Peter Boughton Avatar answered Sep 29 '22 15:09

Peter Boughton