views:

211

answers:

6

What does

/.*=/,'' 

mean in

var id=this.href.replace(/.*=/,'');

?

Full code

function delete_subscriber(){
var id=this.href.replace(/.*=/,'');
this.id='delete_link_'+id;
if(confirm('Are you sure you want to delete this subscriber?'))
$.getJSON('delete.php?ajax=true&id='+id, remove_row);
return false;
}

I assume it is regex, but I have little knowledge about it.

Please help.

+11  A: 

It replaces any character (except line breaks) up to and including the last equals sign with nothing. So given this text:

"I am some text before=and I am some text after"

You'd get:

"and I am some text after"

And given this text:

"I am some text before=and I am in between=and I am after"

you'd get:

"and I am after"
mopoke
Any character except line breaks.
Bart Kiers
+1, up to and including the "last" equal sign.
sberry2A
@Bart K. - good call on the line break. Edited.
mopoke
+4  A: 

It's a regular expression.

The specific syntax here is as follows (comments after the #s):

/    # Begin an expression
.*   # Match all characters
=    # Until an equals sign is met
/    # End the expression

The rest of it is a function that replaces anything that is matched by this regular expression and remove it.

Welbog
not just until an = sign .. but until the last = sign in that line ..
Gaby
As already mentioned under mopoke's answer: by default `.` (DOT) does not match line breaks. Since this answer seems to be the accepted one, I'll mention it here too.
Bart Kiers
+2  A: 

It means, take everything up to and including an equal sign and replace it with nothing, so take something like

http://yourserver.com/blah/blah/blah/id=20 and change it to 20.
sberry2A
+2  A: 

Here is a more detailed explanation:

/.*=/,''
  • // denotes a regular expression
  • . denotes any character except for newline
  • * means 0 or any number of (any character in this case)
  • = literally means the equals symbol
  • '' is the string with which it is being replaced (i.e. nothing)
ternaryOperator
+1  A: 

The /.*=/ is a regular expression which matches: first, a sequence of any character (represented by '.*'), second and finally, matching literally the character equal ('=') as specified by the character itself. The '/' characters delimit the regular expression.

The call to replace() passes an empty replacement string. The purpose of such a call to replace is to delete the matching text. So this invocation deletes the text to the left of the equal sign, and the equal sign itself.

In the context of the overall function, id is assigned the value of all the text after an equal sign in the current page's href. So, this code is unpacking a URL to obtain parameters that are marked by the presence of an equal sign in the URL, it appears.

Heath Hunnicutt
A: 

The slashes begin/end the regular expression. The period matches any character. The asterisk means match any number (zero or more) of the previous expression (any character). The equal sign literally matches an equal sign. So ".*=" means anything (including nothing at all), followed by an equal sign. The second parameter to shits function, '', is an empty string, so the matching string will be deleted (replaced with nothing).

In short, this particular snippet will remove anything before and including an equal sign from the string (this.href, the current URL). So it'd be extracting e.g. the "42" from foo.php?id=42.

HyperHacker