Reputation: 9890
here is my string
str = "asd;images30/127ef-30-30-wm.jpg;59 | asd;images30/127ef-30-30-wm.jpg;60 | "
and regexp is
var re = new RegExp(".*?;.*?;"+$(this).parent().attr("id")+" | ","ig");
and replace function is
str.replace(re,'');
i actually want to remove asd;images30/127ef-30-30-wm.jpg;59 |
from string.
Above Regular expression return
| asd;images30/127ef-30-30-wm.jpg;60 |
whereas expected is
asd;images30/127ef-30-30-wm.jpg;60 |
Upvotes: 0
Views: 79
Reputation: 14345
You have to replace the |
in your RegExp call with \\|
. This is necessary because the single backslash will be ignored (if the character afterward had been used within escape sequences, e.g., if it were an n
, these two would become a newline escape sequence, but for characters not part of escape sequences, a single backslash is ignored), and the additional one will cause the single one to appear. Then it can be used in the regular expression to indicate you are escaping a literal pipe symbol and not the built-in regex "alternate" symbol.
Note that if your id
from jQuery could be non-numeric, you might also need to escape this value. You can escape it with a function like this.
To hard-code an ID here (based on your example, the value was apparently 59), if you remove the double backslashes, it won't remove the first pipe:
str = "asd;images30/127ef-30-30-wm.jpg;59 | asd;images30/127ef-30-30-wm.jpg;60 | ";
var re = new RegExp(".*?;.*?;"+59+" \\| ","ig");
str = str.replace(re,'');
alert(str)
Upvotes: 3