开发者

What does /.*=/,'' mean in var id=this.href.replace(/.*=/,'');?

开发者 https://www.devze.com 2022-12-15 15:18 出处:网络
What does /.*=/,\'\' mean in var id=this.href.replace(/.*=/,\'\'); ? Full code function delete_subscriber(){

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='+i开发者_StackOverflow中文版d, remove_row);
return false;
}

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


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"


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.


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.


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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消