How can I replace the all the text from ~ to | with regex in javascript ?
test.html~1455551818474|test.html
So far I know how to remove after |
\|.*$
I need to find out how to remove between ~ to |
You can use this regex for replacement:
str = 'test.html~1455551818474|test.html';
str = str.replace(/~.*\|/g, '');
//=> test.htmltest.html
If by any chance you want pipe character in output then use:
str = str.replace(/~.*\|/g, '|');
//=> test.html|test.html
Also keep in mind .* is greedy and will find longest match between ~ and | in input in case there are multiple of these.
What about:
var str = "test.html~1455551818474|test.html";
var resp = str.replace(/~/g, "|");
console.log(resp);
var resp = str.replace(/~\d*/g, ""); if you need test.html|test.html
str = str.replace(/~.*\|/g, '')