0

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 | 

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

i am doubtful about space before and after pipline |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.