1

i read the answer regarding the answer 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"
sam
  • 11
  • 1

2 Answers2

1

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
MooGoo
  • 46,796
  • 4
  • 39
  • 32
1

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:

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623