开发者

how can i read the vale of first = sign(or the the value of middle = sign) not the last value

i read the answer regarding the an开发者_StackOverflow社区swer of this 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;
}

but i want the value read through regular expression of the 1st equalsign or middle equal sign not the last for example i want the value "I am some text before" or "and I am in between" from the line below

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


I would suggest using split rather than regex:

var parts = this.href.split('=');
var id = parts[0]; //before first =  "I am some text before"
var id = parts[1]; //after first =  "and I am in between"
var id = parts[2]; //after second =  "and I am after"

If you want text before and after the first = sign:

var id = this.href.split('=').splice(0, 2).join('='); //I am some text before=and I am in between


The short answer

The problem is greediness of .* in your original pattern .*=.

There are two ways to solve this:

  • Use reluctant .*?= instead
  • Use negated character class instead [^=]*=

See also:

  • Difference between .*? and .* for regex (long answer with illustrative examples)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜